Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- cheako@debian:/usr/local/share/perl5/Mojolicious/Plugin$ cat TagHelpersShadow.pm
- package Mojolicious::Plugin::TagHelpers;
- sub _input {
- my ($c, $name) = (shift, shift);
- my %attrs = @_ % 2 ? (value => shift, @_) : @_;
- if (my @values = @{$c->every_param($name)}) {
- # Checkbox or radiobutton
- my $type = $attrs{type} || '';
- if ($type eq 'checkbox' || $type eq 'radio') {
- delete $attrs{checked} if @values;
- my $value = $attrs{value} // 'on';
- $attrs{checked} = undef if grep { $_ eq $value } @values;
- }
- # Others
- else { $attrs{value} = $values[-1] }
- }
- return _hidden_field($c, "tag_helpers_shadow_$name" => $attrs{value}) . _validation($c, $name, 'input', name => $name, %attrs);
- }
- sub _select_field {
- my ($c, $name, $options, %attrs) = (shift, shift, shift, @_);
- my %values = map { $_ => 1 } @{$c->every_param($name)};
- my $groups = '';
- for my $group (@$options) {
- # "optgroup" tag
- if (blessed $group && $group->isa('Mojo::Collection')) {
- my ($label, $values, %attrs) = @$group;
- my $content = join '', map { _option(\%values, $_) } @$values;
- $groups .= _tag('optgroup', label => $label, %attrs, sub {$content});
- }
- # "option" tag
- else { $groups .= _option(\%values, $group) }
- }
- return join('', (map { _hidden_field($c, "tag_helpers_shadow_$name" => $_) } @{$c->every_param($name)} ) ) . _validation($c, $name, 'select', name => $name, %attrs, sub {$groups});
- }
- sub _text_area {
- my ($c, $name) = (shift, shift);
- my $cb = ref $_[-1] eq 'CODE' ? pop : undef;
- my $content = @_ % 2 ? shift : undef;
- $content = $c->param($name) // $content // $cb // '';
- return _hidden_field($c, "tag_helpers_shadow_$name" => $attrs{value}) . _validation($c, $name, 'textarea', name => $name, @_, $content);
- }
- 1;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement