| 1 |
package HTML::FormHandler::Manual::RenderingCookbook; |
| 2 |
# ABSTRACT: rendering recipes |
| 3 |
|
| 4 |
__END__ |
| 5 |
|
| 6 |
=pod |
| 7 |
|
| 8 |
=encoding UTF-8 |
| 9 |
|
| 10 |
=head1 NAME |
| 11 |
|
| 12 |
HTML::FormHandler::Manual::RenderingCookbook - rendering recipes |
| 13 |
|
| 14 |
=head1 VERSION |
| 15 |
|
| 16 |
version 0.410002 |
| 17 |
|
| 18 |
=head1 SYNOPSIS |
| 19 |
|
| 20 |
Collection of rendering recipes |
| 21 |
|
| 22 |
=head1 NAME |
| 23 |
|
| 24 |
HTML::FormHandler::Manual::Rendering::Cookbook |
| 25 |
|
| 26 |
=head1 Recipes |
| 27 |
|
| 28 |
=head2 Custom renderer, custom attributes |
| 29 |
|
| 30 |
You want to be able to specify the attributes that are rendered in the 'td' tag |
| 31 |
of the table renderer... |
| 32 |
|
| 33 |
First make your own copy of 'HTML::FormHandler::Widget::Wrapper::Table, in your |
| 34 |
own name space, and specify that name space in the 'widget_name_space' for the |
| 35 |
form. |
| 36 |
|
| 37 |
Change this line in the Table wrapper: |
| 38 |
|
| 39 |
$output .= '<td>' . $self->do_render_label($result) . '</td>'; |
| 40 |
|
| 41 |
to this: |
| 42 |
|
| 43 |
my $td_attr = process_attrs($self->get_tag('td_attr') || {} ); |
| 44 |
$output .= "<td$td_attr>" . $self->do_render_label($result) . '</td>'; |
| 45 |
|
| 46 |
Now you can specify the attributes for the 'td' tag on a field: |
| 47 |
|
| 48 |
has_field 'foo' => ( tags => { td_attr => { class => ['emph', 'label'] } } ); |
| 49 |
|
| 50 |
=head2 Render a collection of checkboxes like a checkbox group |
| 51 |
|
| 52 |
=head2 Add a 'required' class to labels |
| 53 |
|
| 54 |
Create a custom widget wrapper: |
| 55 |
|
| 56 |
package MyApp::Form::Widget::Wrapper::CustomLabel; |
| 57 |
|
| 58 |
use Moose::Role; |
| 59 |
use HTML::Entities qw( encode_entities ); |
| 60 |
with 'HTML::FormHandler::Widget::Wrapper::Simple'; |
| 61 |
|
| 62 |
sub render_label { |
| 63 |
my ($self) = @_; |
| 64 |
return '<label class="label' . ($self->required ? |
| 65 |
' required' : '') . '" for="' . $self->id . '">' . |
| 66 |
encode_entities($self->loc_label, '<>&') . ': |
| 67 |
</label>'; |
| 68 |
} |
| 69 |
|
| 70 |
Or enable html5 output which adds a 'required' attribute. |
| 71 |
|
| 72 |
Or use the 'html_attributes' callback: |
| 73 |
|
| 74 |
<in a form> |
| 75 |
sub html_attributes { |
| 76 |
my ( $self, $field, $type, $attr ) = @_; |
| 77 |
push @{$attr->{class}}, 'required' |
| 78 |
if ( $type eq 'label' && $field->required ); |
| 79 |
} |
| 80 |
|
| 81 |
=head1 AUTHOR |
| 82 |
|
| 83 |
FormHandler Contributors - see HTML::FormHandler |
| 84 |
|
| 85 |
=head1 COPYRIGHT AND LICENSE |
| 86 |
|
| 87 |
This software is copyright (c) 2017 by Gerda Shank. |
| 88 |
|
| 89 |
This is free software; you can redistribute it and/or modify it under |
| 90 |
the same terms as the Perl 5 programming language system itself. |
| 91 |
|
| 92 |
=cut |