Skip to content

Instantly share code, notes, and snippets.

@ssoriche
Created April 3, 2012 20:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ssoriche/3a1e5549e1c7f80dcdfa to your computer and use it in GitHub Desktop.
Save ssoriche/3a1e5549e1c7f80dcdfa to your computer and use it in GitHub Desktop.
Mojolicious::Plugin::TagHelpers check_box checked from stash
sub _input {
my ($self, $c, $name) = (shift, shift, shift);
say "Attributes: " . p @_;
# Odd
my %attrs;
if (@_ % 2) {
my $value = shift;
%attrs = (@_, value => $value);
}
# Even
else { %attrs = @_ }
say "Attributes: " . p %attrs;
# Value
my @p = $c->param($name) || $c->stash($name); # <---- Change here
# my @p = $c->param($name);
say "Name: $name Parameters: " . p @p;
# Special selection value
my $t = $attrs{type} || '';
if (@p && $t ne 'submit') {
# Checkbox or radiobutton
my $value = $attrs{value} // '';
if ($t eq 'checkbox' || $t eq 'radio') {
say("Value: *$value*");
$attrs{value} = $value;
$attrs{checked} = 'checked' if defined first { $value eq $_ } @p;
}
# Other
else { $attrs{value} = $p[0] }
return $self->_tag('input', name => $name, %attrs);
}
# Empty tag
say("Name: $name");
return $self->_tag('input', name => $name, %attrs);
}
@sshaw
Copy link

sshaw commented Apr 10, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment