| 1 | package HTML::FormHandler::Render::Table; | ||||||||||||||
| 2 | # ABSTRACT: render a form with a table layout | ||||||||||||||
| 3 | 4 | $HTML::FormHandler::Render::Table::VERSION = '0.410001'; | |||||||||||||
| 4 | 4 | ▶ | use Moose::Role; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | |||||||||||||||
| 6 | 4 | with 'HTML::FormHandler::Render::Simple' => | |||||||||||||
| 7 | { -excludes => [ 'render', 'wrap_field', 'render_end', 'render_start' ] }; | ||||||||||||||
| 8 | 4 | ▶ | use HTML::FormHandler::Render::Util ('process_attrs'); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | 4 | ▶ | use HTML::Entities qw( encode_entities ); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | |||||||||||||||
| 11 | |||||||||||||||
| 12 | sub render { | ||||||||||||||
| 13 | 3 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
render: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 14 | |||||||||||||||
| 15 | 3 | my $output = $self->render_start; | |||||||||||||
| 16 | 3 | $output .= $self->render_form_errors; | |||||||||||||
| 17 | 3 | foreach my $field ( $self->sorted_fields ) { | |||||||||||||
| 18 | 21 | $output .= $self->render_field($field); | |||||||||||||
| 19 | } | ||||||||||||||
| 20 | 3 | $output .= $self->render_end; | |||||||||||||
| 21 | 3 | return $output; | |||||||||||||
| 22 | } | ||||||||||||||
| 23 | |||||||||||||||
| 24 | sub render_start { | ||||||||||||||
| 25 | 3 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
render_start: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 26 | |||||||||||||||
| 27 | 3 | my $attrs = process_attrs($self->attributes); | |||||||||||||
| 28 | 3 | return qq{<form$attrs><table>}; | |||||||||||||
| 29 | } | ||||||||||||||
| 30 | |||||||||||||||
| 31 | sub render_form_errors { | ||||||||||||||
| 32 | 3 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
render_form_errors: called CC=3 SCAR=17.3
Pod
undocumented
| |||||||||||||||
| 33 | |||||||||||||||
| 34 | 3 | ▶ | return '' unless $self->has_form_errors; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 35 | 0 | my $output = "\n<tr class=\"form_errors\"><td colspan=\"2\">"; | |||||||||||||
| 36 | $output .= qq{\n<span class="error_message">$_</span>} | ||||||||||||||
| 37 | 0 | for map { encode_entities($_) } $self->all_form_errors; | |||||||||||||
| 38 | 0 | $output .= "\n</td></tr>"; | |||||||||||||
| 39 | 0 | return $output; | |||||||||||||
| 40 | } | ||||||||||||||
| 41 | |||||||||||||||
| 42 | sub render_end { | ||||||||||||||
| 43 | 3 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
render_end: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 44 | 3 | my $output = "</table>\n"; | |||||||||||||
| 45 | 3 | $output .= "</form>\n"; | |||||||||||||
| 46 | 3 | return $output; | |||||||||||||
| 47 | } | ||||||||||||||
| 48 | |||||||||||||||
| 49 | sub wrap_field { | ||||||||||||||
| 50 | 24 | ▶ | my ( $self, $field, $rendered_field ) = @_; | ||||||||||||
|
Subroutine
wrap_field: called CC=7 SCAR=19.5
Pod
undocumented
| |||||||||||||||
| 51 | |||||||||||||||
| 52 | 24 | my $attrs = process_attrs($field->wrapper_attributes); | |||||||||||||
| 53 | 24 | my $output = qq{\n<tr$attrs>}; | |||||||||||||
| 54 | 24 | ▶ | my $l_type = $field->widget eq 'Compound' ? 'legend' : 'label'; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 55 | 24 | ▶ | if ( $l_type eq 'label' ) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 56 | 23 | $output .= '<td>' . $self->render_label($field) . '</td>'; | |||||||||||||
| 57 | } | ||||||||||||||
| 58 | elsif ( $l_type eq 'legend' ) { | ||||||||||||||
| 59 | 1 | $output .= '<td>' . $self->render_label($field) . '</td></tr>'; | |||||||||||||
| 60 | } | ||||||||||||||
| 61 | 24 | ▶ | if ( $l_type ne 'legend' ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 62 | 23 | $output .= '<td>'; | |||||||||||||
| 63 | } | ||||||||||||||
| 64 | 24 | $output .= $rendered_field; | |||||||||||||
| 65 | $output .= qq{\n<span class="error_message">$_</span>} | ||||||||||||||
| 66 | 24 | for map { encode_entities($_) } $field->all_errors; | |||||||||||||
| 67 | 24 | ▶ | if ( $l_type ne 'legend' ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 68 | 23 | $output .= "</td></tr>\n"; | |||||||||||||
| 69 | } | ||||||||||||||
| 70 | 24 | return $output; | |||||||||||||
| 71 | } | ||||||||||||||
| 72 | |||||||||||||||
| 73 | 4 | ▶ | use namespace::autoclean; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 74 | 4 | 1; | |||||||||||||
| 75 | |||||||||||||||
| 76 | __END__ | ||||||||||||||
| 77 | |||||||||||||||
| 78 | =pod | ||||||||||||||
| 79 | |||||||||||||||
| 80 | =encoding UTF-8 | ||||||||||||||
| 81 | |||||||||||||||
| 82 | =head1 NAME | ||||||||||||||
| 83 | |||||||||||||||
| 84 | HTML::FormHandler::Render::Table - render a form with a table layout | ||||||||||||||
| 85 | |||||||||||||||
| 86 | =head1 VERSION | ||||||||||||||
| 87 | |||||||||||||||
| 88 | version 0.410001 | ||||||||||||||
| 89 | |||||||||||||||
| 90 | =head1 SYNOPSIS | ||||||||||||||
| 91 | |||||||||||||||
| 92 | Include this role in a form: | ||||||||||||||
| 93 | |||||||||||||||
| 94 | package MyApp::Form::User; | ||||||||||||||
| 95 | use Moose; | ||||||||||||||
| 96 | with 'HTML::FormHandler::Render::Table'; | ||||||||||||||
| 97 | |||||||||||||||
| 98 | Use in a template: | ||||||||||||||
| 99 | |||||||||||||||
| 100 | [% form.render %] | ||||||||||||||
| 101 | |||||||||||||||
| 102 | =head1 AUTHOR | ||||||||||||||
| 103 | |||||||||||||||
| 104 | FormHandler Contributors - see HTML::FormHandler | ||||||||||||||
| 105 | |||||||||||||||
| 106 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||
| 107 | |||||||||||||||
| 108 | This software is copyright (c) 2017 by Gerda Shank. | ||||||||||||||
| 109 | |||||||||||||||
| 110 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||
| 111 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||
| 112 | |||||||||||||||
| 113 | =cut | ||||||||||||||