Skip to content

Commit

Permalink
handle rendering result errors
Browse files Browse the repository at this point in the history
  • Loading branch information
gshank committed Jan 22, 2012
1 parent e28b800 commit e46c199
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 11 deletions.
24 changes: 17 additions & 7 deletions lib/HTML/FormHandler/Field.pm
Original file line number Diff line number Diff line change
Expand Up @@ -962,14 +962,24 @@ sub label_attributes {
$attr = $self->form->field_html_attributes($self, 'label', $attr) if $self->form;
return $attr;
}

sub wrapper_attributes {
my $self = shift;
my $attr = {%{$self->wrapper_attr}};
if( ! exists $attr->{class} && defined $self->css_class ) {
$attr->{class} = $self->css_class;
}
$attr = $self->form->field_html_attributes($self, 'label', $attr) if $self->form;
return $attr;
my ( $self, $result ) = @_;
$result ||= $self->result;
my $attr = {%{$self->wrapper_attr}};
if( ! exists $attr->{class} && defined $self->css_class ) {
$attr->{class} = $self->css_class;
}
if( $result->has_errors ) {
if( ref $attr->{class} eq 'ARRAY' ) {
push @{$attr->{class}}, 'error';
}
else {
$attr->{class} .= $attr->{class} ? ' error' : 'error';
}
}
$attr = $self->form->field_html_attributes($self, 'label', $attr) if $self->form;
return $attr;
}

#=====================
Expand Down
2 changes: 1 addition & 1 deletion lib/HTML/FormHandler/Widget/Wrapper/Simple.pm
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ sub wrap_field {
$output .= $start_tag;
}
else {
$output .= "<$tag" . process_attrs( $self->wrapper_attributes ) . ">";
$output .= "<$tag" . process_attrs( $self->wrapper_attributes($result) ) . ">";
}

if ( $self->has_flag('is_compound') ) {
Expand Down
2 changes: 1 addition & 1 deletion lib/HTML/FormHandler/Widget/Wrapper/SimpleInline.pm
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ sub wrap_field {
$output .= $start_tag;
}
else {
$output .= "<$tag" . process_attrs( $self->wrapper_attributes ) . ">";
$output .= "<$tag" . process_attrs( $self->wrapper_attributes($result) ) . ">";
}

if ( !$self->has_flag('no_render_label') && length( $self->label ) > 0 ) {
Expand Down
2 changes: 1 addition & 1 deletion lib/HTML/FormHandler/Widget/Wrapper/Table.pm
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use HTML::FormHandler::Render::Util ('process_attrs');
sub wrap_field {
my ( $self, $result, $rendered_widget ) = @_;

my $output = "\n<tr" . process_attrs($self->wrapper_attributes) . ">";
my $output = "\n<tr" . process_attrs($self->wrapper_attributes($result)) . ">";
if ( $self->has_flag('is_compound') ) {
$output .= '<td>' . $self->render_label . '</td></tr>';
}
Expand Down
2 changes: 1 addition & 1 deletion lib/HTML/FormHandler/Widget/Wrapper/TableInline.pm
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ sub wrap_field {

return $rendered_widget if $self->has_flag('is_compound');

my $output = "\n<tr" . process_attrs($self->wrapper_attributes) . ">";
my $output = "\n<tr" . process_attrs($self->wrapper_attributes($result)) . ">";
if ( !$self->has_flag('no_render_label') && length( $self->label ) > 0 ) {
$output .= '<td>' . $self->render_label . '</td>';
}
Expand Down
38 changes: 38 additions & 0 deletions t/render_errors.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use strict;
use warnings;
use Test::More;

{
package Test::Form;
use HTML::FormHandler::Moose;
extends 'HTML::FormHandler';

has '+name' => ( default => 'test_errors' );
has_field 'foo' => ( required => 1 );
has_field 'bar' => ( type => 'Integer' );

}

my $form = Test::Form->new;
$form->process( params => { bar => 'abc' } );

is( $form->num_errors, 2, 'got two errors' );

my $expected = '<form id="test_errors" method="post" >
<fieldset class="main_fieldset">
<div class="error"><label class="label" for="foo">Foo: </label><input type="text" name="foo" id="foo" value="" />
<span class="error_message">Foo field is required</span></div>
<div class="error"><label class="label" for="bar">Bar: </label><input type="text" name="bar" id="bar" size="8" value="abc" />
<span class="error_message">Value must be an integer</span></div>
</fieldset></form>';
my $rendered = $form->render;

SKIP: {
skip 'Install HTML::TreeBuilder to test TT Result', 3
unless eval { require HTML::TreeBuilder && $HTML::TreeBuilder::VERSION >= 3.23 };
my $exp_tree = HTML::TreeBuilder->new_from_content($expected);
my $got_tree = HTML::TreeBuilder->new_from_content($rendered);
is($exp_tree->as_HTML, $got_tree->as_HTML, 'error rendering matches expected');
}

done_testing;

0 comments on commit e46c199

Please sign in to comment.