| 1 |
use strict; |
| 2 |
use warnings; |
| 3 |
use Test::More; |
| 4 |
|
| 5 |
use HTML::FormHandler::I18N; |
| 6 |
$ENV{LANGUAGE_HANDLE} = 'en_en'; |
| 7 |
|
| 8 |
{ |
| 9 |
package Test::SingleBool; |
| 10 |
use HTML::FormHandler::Moose; |
| 11 |
extends 'HTML::FormHandler'; |
| 12 |
|
| 13 |
has_field 'opt_in' => ( type => 'Boolean', required => 1 ); |
| 14 |
} |
| 15 |
|
| 16 |
{ |
| 17 |
my $form = Test::SingleBool->new; |
| 18 |
ok( $form, 'form built' ); |
| 19 |
$form->process( params => {} ); |
| 20 |
ok( !$form->ran_validation, 'form did not run validation' ); |
| 21 |
|
| 22 |
my $test = 'POST'; |
| 23 |
$form->process( posted => ($test eq 'POST'), params => {} ); |
| 24 |
ok( $form->ran_validation, 'form did run validation' ); |
| 25 |
ok( $form->has_errors, 'form has errors' ); |
| 26 |
|
| 27 |
my @errors = $form->errors; |
| 28 |
is( scalar @errors, 1, 'form has an error' ); |
| 29 |
is( $errors[0], 'Opt in field is required', 'error message is correct' ); |
| 30 |
} |
| 31 |
|
| 32 |
{ |
| 33 |
package Test::SingleBoolCompound; |
| 34 |
use HTML::FormHandler::Moose; |
| 35 |
extends 'HTML::FormHandler'; |
| 36 |
|
| 37 |
|
| 38 |
has_field 'a' => ( type => 'Compound', required => 1 ); |
| 39 |
has_field 'a.opt_in' => ( type => 'Boolean', required => 1 ); |
| 40 |
} |
| 41 |
|
| 42 |
{ |
| 43 |
my $form = Test::SingleBoolCompound->new; |
| 44 |
ok( $form, 'form built' ); |
| 45 |
$form->process( params => {} ); |
| 46 |
ok( !$form->ran_validation, 'form did not run validation' ); |
| 47 |
|
| 48 |
my $test = 'POST'; |
| 49 |
$form->process( posted => ($test eq 'POST'), params => {} ); |
| 50 |
ok( $form->ran_validation, 'form did run validation' ); |
| 51 |
ok( $form->has_errors, 'form has errors' ); |
| 52 |
|
| 53 |
my @errors = $form->errors; |
| 54 |
is( scalar @errors, 1, 'form has an error' ); |
| 55 |
is( $errors[0], 'A field is required', 'error message is correct' ); |
| 56 |
} |
| 57 |
|
| 58 |
done_testing; |
| 59 |
|