Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds ability to set form-read-only to a ::Form object and have the form render that field as disabled #51

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion lib/Cro/WebApp/Form.pm6
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ my role FormProperties {
has Int $.webapp-form-maxlength is rw;
has Real $.webapp-form-min is rw;
has Real $.webapp-form-max is rw;
has Bool $.webapp-form-ro is rw;
has List @.webapp-form-validations;
}

Expand Down Expand Up @@ -155,6 +156,11 @@ multi trait_mod:<is>(Attribute:D $attr, Real :$min! --> Nil) is export {
$attr.webapp-form-min = $min;
}

multi trait_mod:<is>(Attribute:D $attr, Bool :$form-read-only! --> Nil) is export {
ensure-attr-state($attr);
$attr.webapp-form-ro = $form-read-only;
}

#| Set the maximum numeric value of an input field
multi trait_mod:<is>(Attribute:D $attr, Real :$max! --> Nil) is export {
ensure-attr-state($attr);
Expand Down Expand Up @@ -292,9 +298,11 @@ role Cro::WebApp::Form {
my %form-data := $body.hash;
my %values;
my %unparseable;

for self.^attributes.grep(*.has_accessor) -> Attribute $attr {
my $name = $attr.name.substr(2);
my $value := %form-data{$name};

if $attr.type ~~ Positional {
my $value-type = $attr.type.of;
my @values := $value ~~ Cro::HTTP::MultiValue ?? $value.list !!
Expand Down Expand Up @@ -393,6 +401,7 @@ role Cro::WebApp::Form {
(with $attr.?webapp-form-placeholder { placeholder => $_ }),
required => ?$attr.required,
type => $control-type,
read-only => $attr.?webapp-form-ro,
%properties;
if %validation-by-control{$name} -> @errors {
self!set-control-validation(%control, @errors)
Expand Down Expand Up @@ -487,7 +496,9 @@ role Cro::WebApp::Form {
method !add-current-value(Attribute $attr, %properties? is copy) {
with $attr.get_value(self) {
when Date { %properties<value> = .yyyy-mm-dd; }
when DateTime { %properties<value> = .Str; }

when DateTime { %properties<value> .= Str }

default { %properties<value> = $_; }
}
orwith %!unparseable{$attr.name.substr(2)} {
Expand Down
16 changes: 8 additions & 8 deletions resources/prelude.crotmp
Original file line number Diff line number Diff line change
Expand Up @@ -46,54 +46,54 @@
<?{ .type eq 'text' || .type eq 'email' || .type eq 'search' || .type eq 'url' || .type eq 'tel' }>
<div<&class($input-group-class)>>
<label for="<.name>"<&class($input-label-class)>><.label></label>
<input type="<.type>" name="<.name>" id="<.name>"<&control-class($_, $input-control-class, :$is-invalid-class)><&str-opts($_)><?.<value>.defined()> value="<.value>"</?>>
<input type="<.type>" name="<.name>" <?.read-only>disabled</?> id="<.name>"<&control-class($_, $input-control-class, :$is-invalid-class)><&str-opts($_)><?.<value>.defined()> value="<.value>"</?>>
<&control-help($_, :$help-class)>
<&control-validation-message($_, :$invalid-feedback-class)>
</div>
</?>
<?{ .type eq 'file' }>
<div<&class($input-group-class)>>
<label for="<.name>"<&class($input-label-class)>><.label></label>
<input type="<.type>" name="<.name>" id="<.name>"<&control-class($_, $input-control-class, :$is-invalid-class)><&str-opts($_)>>
<input type="<.type>" name="<.name>" <?.read-only>disabled</?> id="<.name>"<&control-class($_, $input-control-class, :$is-invalid-class)><&str-opts($_)>>
<&control-help($_, :$help-class)>
<&control-validation-message($_, :$invalid-feedback-class)>
</div>
</?>
<?{ .type eq 'number' }>
<div<&class($input-group-class)>>
<label for="<.name>"<&class($input-label-class)>><.label></label>
<input type="number" name="<.name>" id="<.name>"<&control-class($_, $input-control-class, :$is-invalid-class)><&num-opts($_)><?.<value>.defined()> value="<.value>"</?>>
<input type="number" name="<.name>" <?.read-only>disabled</?> id="<.name>"<&control-class($_, $input-control-class, :$is-invalid-class)><&num-opts($_)><?.<value>.defined()> value="<.value>"</?>>
<&control-help($_, :$help-class)>
<&control-validation-message($_, :$invalid-feedback-class)>
</div>
</?>
<?{ .type eq 'color' || .type eq 'date' || .type eq 'datetime-local' || .type eq 'month' || .type eq 'time' || .type eq 'week' }>
<div<&class($input-group-class)>>
<label for="<.name>"<&class($input-label-class)>><.label></label>
<input type="<.type>" name="<.name>" id="<.name>"<&control-class($_, $input-control-class, :$is-invalid-class)><?.<value>.defined()> value="<.value>"</?>>
<input type="<.type>" name="<.name>" <?.read-only>disabled</?> id="<.name>"<&control-class($_, $input-control-class, :$is-invalid-class)><?.<value>.defined()> value="<.value>"</?>>
<&control-help($_, :$help-class)>
<&control-validation-message($_, :$invalid-feedback-class)>
</div>
</?>
<?{ .type eq 'password' }>
<div<&class($input-group-class)>>
<label for="<.name>"<&class($input-label-class)>><.label></label>
<input type="password" name="<.name>" id="<.name>"<&control-class($_, $input-control-class, :$is-invalid-class)>>
<input type="password" name="<.name>" <?.read-only>disabled</?> id="<.name>"<&control-class($_, $input-control-class, :$is-invalid-class)>>
<&control-help($_, :$help-class)>
<&control-validation-message($_, :$invalid-feedback-class)>
</div>
</?>
<?{ .type eq 'textarea' }>
<div<&class($input-group-class)>>
<label for="<.name>"<&class($input-label-class)>><.label></label>
<textarea name="<.name>" id="<.name>"<&control-class($_, $input-control-class, :$is-invalid-class)><&rows-cols(.rows, .cols)><&str-opts($_)>><?.<value>.defined()><.value></?></textarea>
<textarea name="<.name>" <?.read-only>disabled</?> id="<.name>"<&control-class($_, $input-control-class, :$is-invalid-class)><&rows-cols(.rows, .cols)><&str-opts($_)>><?.<value>.defined()><.value></?></textarea>
<&control-help($_, :$help-class)>
<&control-validation-message($_, :$invalid-feedback-class)>
</div>
</?>
<?{ .type eq 'checkbox' }>
<div<&class($check-group-class)>>
<input type="checkbox" name="<.name>" id="<.name>"<&control-class($_, $check-control-class, :$is-invalid-class)><?.<value>> checked</?>>
<input type="checkbox" name="<.name>" <?.read-only>disabled</?> id="<.name>"<&control-class($_, $check-control-class, :$is-invalid-class)><?.<value>> checked</?>>
<label for="<.name>"<&class($check-label-class)>><.label></label>
<&control-help($_, :$help-class)>
<&control-validation-message($_, :$invalid-feedback-class)>
Expand All @@ -102,7 +102,7 @@
<?{ .type eq 'select' }>
<div<&class($input-group-class)>>
<label for="<.name>"<&class($input-label-class)>><.label></label>
<select <?.multi>multiple</?> name="<.name>" id="<.name>"<&control-class($_, $input-control-class, :$is-invalid-class)>>
<select <?.multi>multiple</?> name="<.name>" <?.read-only>disabled</?> id="<.name>"<&control-class($_, $input-control-class, :$is-invalid-class)>>
<@options : $opt>
<option value="<$opt.[0]>"<?{ $opt.[2] }> selected="selected"</?>><$opt.[1]></option>
</@>
Expand Down