PageRenderTime 10ms CodeModel.GetById 1ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 1ms

/xt/form_errors.t

http://github.com/gshank/html-formhandler
Unknown | 48 lines | 42 code | 6 blank | 0 comment | 0 complexity | db604b1e02f1dbcde38ce6fe74c935d7 MD5 | raw file
 1use strict;
 2use warnings;
 3use Test::More;
 4
 5{
 6    package Test::Form;
 7    use HTML::FormHandler::Moose;
 8    extends 'HTML::FormHandler';
 9
10    has 'secret' => ( is => 'rw', default => 'wrong' );
11
12    has_field 'foo';
13    has_field 'bar';
14
15    sub validate_foo {
16        my ( $self, $field ) = @_;
17        $field->add_error('Not a valid foo')
18            if( $field->value eq 'test' );
19    }
20    sub validate_bar {
21        my ( $self, $field ) = @_;
22        $field->add_error('Not a valid bar')
23            if( $field->value eq 'bad_bar' );
24    }
25    sub validate {
26        my $self = shift;
27        $self->add_form_error('Try again')
28           if( $self->field('foo')->value ne $self->secret );
29    }
30}
31
32my $form = Test::Form->new;
33ok( $form, 'form builds' );
34$form->process( params => {} );
35my $params = {
36    foo => 'test',
37    bar => 'bad_bar',
38};
39$form->process( secret => 'yikes', params => $params );
40ok( !$form->validated, 'form did not validate' );
41$form->process( secret => 'my_bar', params => { bar => 'my_bar', foo => 'my_foo' } );
42my @errors = $form->errors;
43is( $errors[0], 'Try again', 'form error' );
44$form->process( secret => 'my_foo', params => { bar => 'my_bar', foo => 'my_foo' } );
45ok( $form->validated, 'form validated' );
46ok( !$form->has_form_errors, 'form errors are gone' );
47
48done_testing;