| 1 |
use strict; |
| 2 |
use warnings; |
| 3 |
use Test::More; |
| 4 |
|
| 5 |
# shows behavior of required flag in compound fields |
| 6 |
{ |
| 7 |
package MyApp::Form::User; |
| 8 |
use HTML::FormHandler::Moose; |
| 9 |
extends 'HTML::FormHandler'; |
| 10 |
|
| 11 |
has '+name' => ( default => 'userform' ); |
| 12 |
|
| 13 |
has_field 'name'; |
| 14 |
has_field 'email'; |
| 15 |
has_field 'address' => ( type => 'Compound' ); |
| 16 |
has_field 'address.city' => ( required => 1 ); |
| 17 |
has_field 'address.state' => ( required => 1 ); |
| 18 |
|
| 19 |
} |
| 20 |
|
| 21 |
my $form = MyApp::Form::User->new; |
| 22 |
my $params = { |
| 23 |
name => 'John Doe', |
| 24 |
email => '[email protected]', |
| 25 |
}; |
| 26 |
|
| 27 |
# no errors if compound subfields are required but missing |
| 28 |
# and compound field is not required |
| 29 |
$form->process( params => $params ); |
| 30 |
ok( $form->validated, 'no errors in form' ); |
| 31 |
|
| 32 |
# error if one field is entered and not the other |
| 33 |
# and compound field is not required |
| 34 |
$form->process( params => { %$params, 'address.city' => 'New York' } ); |
| 35 |
ok( $form->has_errors, 'error with one field filled' ); |
| 36 |
|
| 37 |
# errors if compound subfields are required & compound is required |
| 38 |
$form->process( update_field_list => { address => { required => 1 } }, |
| 39 |
params => $params ); |
| 40 |
ok( $form->has_errors, 'errors in form' ); |
| 41 |
|
| 42 |
# tests that errors are propagated up the tree, and aren't duplicated |
| 43 |
{ |
| 44 |
package MyApp::Form::Test; |
| 45 |
use HTML::FormHandler::Moose; |
| 46 |
extends 'HTML::FormHandler'; |
| 47 |
|
| 48 |
has_field 'name'; |
| 49 |
has_field 'comp1' => ( type => 'Compound' ); |
| 50 |
has_field 'comp1.comment'; |
| 51 |
has_field 'comp1.comp2' => ( type => 'Compound' ); |
| 52 |
has_field 'comp1.comp2.one'; |
| 53 |
has_field 'comp1.comp2.two' => ( type => 'PosInteger' ); |
| 54 |
|
| 55 |
} |
| 56 |
|
| 57 |
$form = MyApp::Form::Test->new; |
| 58 |
ok( $form ); |
| 59 |
$form->process; |
| 60 |
$params = { |
| 61 |
name => 'test', |
| 62 |
'comp1.comment' => 'This is a test', |
| 63 |
'comp1.comp2.one' => 1, |
| 64 |
'comp1.comp2.two' => 'abc', |
| 65 |
}; |
| 66 |
|
| 67 |
$form->process( params => $params ); |
| 68 |
ok( $form->has_errors, 'form has errors' ); |
| 69 |
my @errors = $form->errors; |
| 70 |
is( scalar @errors, 2, 'right number of errors' ); |
| 71 |
done_testing; |