| 1 |
package HTML::FormHandler::Render::Table; |
| 2 |
# ABSTRACT: render a form with a table layout |
| 3 |
$HTML::FormHandler::Render::Table::VERSION = '0.410002'; |
| 4 |
use Moose::Role; |
| 5 |
|
| 6 |
with 'HTML::FormHandler::Render::Simple' => |
| 7 |
{ -excludes => [ 'render', 'wrap_field', 'render_end', 'render_start' ] }; |
| 8 |
use HTML::FormHandler::Render::Util ('process_attrs'); |
| 9 |
use HTML::Entities qw( encode_entities ); |
| 10 |
|
| 11 |
|
| 12 |
sub render { |
| 13 |
my $self = shift; |
| 14 |
|
| 15 |
my $output = $self->render_start; |
| 16 |
$output .= $self->render_form_errors; |
| 17 |
foreach my $field ( $self->sorted_fields ) { |
| 18 |
$output .= $self->render_field($field); |
| 19 |
} |
| 20 |
$output .= $self->render_end; |
| 21 |
return $output; |
| 22 |
} |
| 23 |
|
| 24 |
sub render_start { |
| 25 |
my $self = shift; |
| 26 |
|
| 27 |
my $attrs = process_attrs($self->attributes); |
| 28 |
return qq{<form$attrs><table>}; |
| 29 |
} |
| 30 |
|
| 31 |
sub render_form_errors { |
| 32 |
my $self = shift; |
| 33 |
|
| 34 |
return '' unless $self->has_form_errors; |
| 35 |
my $output = "\n<tr class=\"form_errors\"><td colspan=\"2\">"; |
| 36 |
$output .= qq{\n<span class="error_message">$_</span>} |
| 37 |
for map { encode_entities($_) } $self->all_form_errors; |
| 38 |
$output .= "\n</td></tr>"; |
| 39 |
return $output; |
| 40 |
} |
| 41 |
|
| 42 |
sub render_end { |
| 43 |
my $self = shift; |
| 44 |
my $output = "</table>\n"; |
| 45 |
$output .= "</form>\n"; |
| 46 |
return $output; |
| 47 |
} |
| 48 |
|
| 49 |
sub wrap_field { |
| 50 |
my ( $self, $field, $rendered_field ) = @_; |
| 51 |
|
| 52 |
my $attrs = process_attrs($field->wrapper_attributes); |
| 53 |
my $output = qq{\n<tr$attrs>}; |
| 54 |
my $l_type = $field->widget eq 'Compound' ? 'legend' : 'label'; |
| 55 |
if ( $l_type eq 'label' ) { |
| 56 |
$output .= '<td>' . $self->render_label($field) . '</td>'; |
| 57 |
} |
| 58 |
elsif ( $l_type eq 'legend' ) { |
| 59 |
$output .= '<td>' . $self->render_label($field) . '</td></tr>'; |
| 60 |
} |
| 61 |
if ( $l_type ne 'legend' ) { |
| 62 |
$output .= '<td>'; |
| 63 |
} |
| 64 |
$output .= $rendered_field; |
| 65 |
$output .= qq{\n<span class="error_message">$_</span>} |
| 66 |
for map { encode_entities($_) } $field->all_errors; |
| 67 |
if ( $l_type ne 'legend' ) { |
| 68 |
$output .= "</td></tr>\n"; |
| 69 |
} |
| 70 |
return $output; |
| 71 |
} |
| 72 |
|
| 73 |
use namespace::autoclean; |
| 74 |
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.410002 |
| 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 |