/lib/HTML/FormHandler/Widget/Form/Table.pm

http://github.com/gshank/html-formhandler · Perl · 49 lines · 35 code · 13 blank · 1 comment · 1 complexity · 47759fe34ea115e78efbf5fde65c4fae MD5 · raw file

  1. package HTML::FormHandler::Widget::Form::Table;
  2. # ABSTRACT: render a form with a table layout
  3. use Moose::Role;
  4. with 'HTML::FormHandler::Widget::Form::Simple' =>
  5. { -excludes => [ 'render_start', 'render_end', 'render_form_errors' ] };
  6. use HTML::FormHandler::Render::Util ('process_attrs');
  7. =head1 SYNOPSIS
  8. Set in your form:
  9. has '+widget_form' => ( default => 'Table' );
  10. Use in a template:
  11. [% form.render %]
  12. =cut
  13. sub render_start {
  14. my ( $self, $result ) = @_;
  15. $result ||= $self->result;
  16. my $fattrs = process_attrs($self->attributes($result));
  17. my $wattrs = process_attrs($self->form_wrapper_attributes($result));
  18. return qq{<form$fattrs><table$wattrs>\n};
  19. }
  20. sub render_form_errors {
  21. my ( $self, $result ) = @_;
  22. return '' unless $result->has_form_errors;
  23. my $output = "\n<tr class=\"form_errors\"><td colspan=\"2\">";
  24. $output .= qq{\n<span class="error_message">$_</span>}
  25. for $result->all_form_errors;
  26. $output .= "\n</td></tr>";
  27. return $output;
  28. }
  29. sub render_end {
  30. my $self = shift;
  31. my $output .= "</table>\n";
  32. $output .= "</form>\n";
  33. return $output;
  34. }
  35. use namespace::autoclean;
  36. 1;