Skip to content

Commit

Permalink
improve check_box, radio_button and select_field tag helpers to handl…
Browse files Browse the repository at this point in the history
…e the attributes "checked" and "selected" correctly
  • Loading branch information
kraih committed Aug 15, 2015
1 parent 4f5f966 commit a244d56
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 7 deletions.
4 changes: 3 additions & 1 deletion Changes
@@ -1,5 +1,7 @@

6.16 2015-08-14
6.16 2015-08-16
- Improved check_box, radio_button and select_field tag helpers to handle the
attributes "checked" and "selected" correctly.

6.15 2015-08-13
- Removed deprecated build_body and build_headers methods from Mojo::Content.
Expand Down
19 changes: 16 additions & 3 deletions lib/Mojolicious/Plugin/TagHelpers.pm
Expand Up @@ -75,6 +75,7 @@ sub _input {
my $value = $attrs{value} // '';
if ($type eq 'checkbox' || $type eq 'radio') {
$attrs{value} = $value;
delete $attrs{checked} if @values;
$attrs{checked} = undef if grep { $_ eq $value } @values;
}

Expand Down Expand Up @@ -117,10 +118,13 @@ sub _link_to {

sub _option {
my ($values, $pair) = @_;

$pair = [$pair => $pair] unless ref $pair eq 'ARRAY';
my %attrs = (value => $pair->[1]);
$attrs{selected} = undef if exists $values->{$pair->[1]};
return _tag('option', %attrs, @$pair[2 .. $#$pair], $pair->[0]);
my %attrs = (value => $pair->[1], @$pair[2 .. $#$pair]);
delete $attrs{selected} if keys %$values;
$attrs{selected} = undef if $values->{$pair->[1]};

return _tag('option', %attrs, $pair->[0]);
}

sub _select_field {
Expand Down Expand Up @@ -253,12 +257,14 @@ L<Mojolicious::Plugin::TagHelpers> implements the following helpers.
%= check_box employed => 1
%= check_box employed => 1, disabled => 'disabled'
%= check_box employed => 1, checked => undef
Generate C<input> tag of type C<checkbox>. Previous input values will
automatically get picked up and shown as default.
<input name="employed" type="checkbox" value="1">
<input disabled="disabled" name="employed" type="checkbox" value="1">
<input checked name="employed" type="checkbox" value="1">
=head2 color_field
Expand Down Expand Up @@ -514,12 +520,14 @@ Generate C<input> tag of type C<password>.
%= radio_button country => 'germany'
%= radio_button country => 'germany', id => 'foo'
%= radio_button country => 'germany', checked => undef
Generate C<input> tag of type C<radio>. Previous input values will
automatically get picked up and shown as default.
<input name="country" type="radio" value="germany">
<input id="foo" name="country" type="radio" value="germany">
<input checked id="foo" name="country" type="radio" value="germany">
=head2 range_field
Expand Down Expand Up @@ -552,6 +560,7 @@ automatically get picked up and shown as default.
%= select_field country => [qw(de en)]
%= select_field country => [[Germany => 'de'], 'en'], id => 'eu'
%= select_field country => [[Germany => 'de', disabled => 'disabled'], 'en']
%= select_field country => [[Germany => 'de', selected => undef], 'en']
%= select_field country => [c(EU => [[Germany => 'de'], 'en'], id => 'eu')]
%= select_field country => [c(EU => [qw(de en)]), c(Asia => [qw(cn jp)])]
Expand All @@ -571,6 +580,10 @@ get picked up and shown as default.
<option disabled="disabled" value="de">Germany</option>
<option value="en">en</option>
</select>
<select name="country">
<option selected value="de">Germany</option>
<option value="en">en</option>
</select>
<select name="country">
<optgroup id="eu" label="EU">
<option value="de">Germany</option>
Expand Down
29 changes: 26 additions & 3 deletions t/mojolicious/tag_helper_lite_app.t
Expand Up @@ -208,6 +208,7 @@ $t->get_ok('/multibox')->status_is(200)->content_is(<<EOF);
<form action="/multibox">
<input name="foo" type="checkbox" value="one">
<input name="foo" type="checkbox" value="two">
<input checked name="foo" type="checkbox" value="three">
<input type="submit" value="Ok">
</form>
EOF
Expand All @@ -217,15 +218,17 @@ $t->get_ok('/multibox?foo=two')->status_is(200)->content_is(<<EOF);
<form action="/multibox">
<input name="foo" type="checkbox" value="one">
<input checked name="foo" type="checkbox" value="two">
<input name="foo" type="checkbox" value="three">
<input type="submit" value="Ok">
</form>
EOF

# Checkboxes with one right and one wrong value
$t->get_ok('/multibox?foo=one&foo=three')->status_is(200)->content_is(<<EOF);
$t->get_ok('/multibox?foo=one&foo=four')->status_is(200)->content_is(<<EOF);
<form action="/multibox">
<input checked name="foo" type="checkbox" value="one">
<input name="foo" type="checkbox" value="two">
<input name="foo" type="checkbox" value="three">
<input type="submit" value="Ok">
</form>
EOF
Expand All @@ -235,6 +238,7 @@ $t->get_ok('/multibox?foo=bar')->status_is(200)->content_is(<<EOF);
<form action="/multibox">
<input name="foo" type="checkbox" value="one">
<input name="foo" type="checkbox" value="two">
<input name="foo" type="checkbox" value="three">
<input type="submit" value="Ok">
</form>
EOF
Expand All @@ -244,6 +248,7 @@ $t->get_ok('/multibox?foo=two&foo=one')->status_is(200)->content_is(<<EOF);
<form action="/multibox">
<input checked name="foo" type="checkbox" value="one">
<input checked name="foo" type="checkbox" value="two">
<input name="foo" type="checkbox" value="three">
<input type="submit" value="Ok">
</form>
EOF
Expand Down Expand Up @@ -337,11 +342,15 @@ $t->put_ok('/selection')->status_is(200)
. '<option value="b">b</option>'
. '</optgroup>'
. "</select>\n "
. '<select name="h">'
. '<option selected value="i">I</option>'
. '<option value="j">J</option>'
. "</select>\n "
. '<input type="submit" value="Ok">'
. "\n</form>\n");

# Selection with values
$t->put_ok('/selection?a=e&foo=bar&bar=baz&yada=b')->status_is(200)
$t->put_ok('/selection?a=e&foo=bar&bar=baz&yada=b&h=j')->status_is(200)
->content_is("<form action=\"/selection?_method=PUT\" method=\"POST\">\n "
. '<select name="a">'
. '<option value="b">b</option>'
Expand All @@ -366,11 +375,15 @@ $t->put_ok('/selection?a=e&foo=bar&bar=baz&yada=b')->status_is(200)
. '<option selected value="b">b</option>'
. '</optgroup>'
. "</select>\n "
. '<select name="h">'
. '<option value="i">I</option>'
. '<option selected value="j">J</option>'
. "</select>\n "
. '<input type="submit" value="Ok">'
. "\n</form>\n");

# Selection with multiple values
$t->put_ok('/selection?foo=bar&a=e&foo=baz&bar=d&yada=a&yada=b')
$t->put_ok('/selection?foo=bar&a=e&foo=baz&bar=d&yada=a&yada=b&h=i&h=j')
->status_is(200)
->content_is("<form action=\"/selection?_method=PUT\" method=\"POST\">\n "
. '<select name="a">'
Expand All @@ -396,6 +409,10 @@ $t->put_ok('/selection?foo=bar&a=e&foo=baz&bar=d&yada=a&yada=b')
. '<option selected value="b">b</option>'
. '</optgroup>'
. "</select>\n "
. '<select name="h">'
. '<option selected value="i">I</option>'
. '<option selected value="j">J</option>'
. "</select>\n "
. '<input type="submit" value="Ok">'
. "\n</form>\n");

Expand Down Expand Up @@ -425,6 +442,10 @@ $t->put_ok('/selection?preselect=1')->status_is(200)
. '<option value="b">b</option>'
. '</optgroup>'
. "</select>\n "
. '<select name="h">'
. '<option selected value="i">I</option>'
. '<option value="j">J</option>'
. "</select>\n "
. '<input type="submit" value="Ok">'
. "\n</form>\n");

Expand Down Expand Up @@ -561,6 +582,7 @@ __DATA__
%= form_for multibox => begin
%= check_box foo => 'one'
%= check_box foo => 'two'
%= check_box foo => 'three', checked => undef
%= submit_button
%= end
Expand Down Expand Up @@ -601,6 +623,7 @@ __DATA__
%= select_field foo => [qw(bar baz)], multiple => 'multiple'
%= select_field bar => [['D' => 'd', disabled => 'disabled'], 'baz']
%= select_field yada => [c(test => [qw(a b)], class => 'x')];
%= select_field h => [['I' => 'i', selected => undef], ['J' => 'j']]
%= submit_button
%= end
Expand Down

0 comments on commit a244d56

Please sign in to comment.