1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
|
use Test::More;
use lib 't/lib';
use_ok( 'HTML::FormHandler::Field::Duration');
use HTML::FormHandler::I18N;
$ENV{LANGUAGE_HANDLE} = 'en_en';
my $field = HTML::FormHandler::Field::Duration->new( name => 'duration' );
$field->build_result;
ok( $field, 'get compound field');
my $input = {
hours => 1,
minutes => 2,
};
$field->_set_input($input);
is_deeply( $field->input, $input, 'field input is correct');
is_deeply( $field->fif, $input, 'field fif is same');
{
package Duration::Form;
use HTML::FormHandler::Moose;
extends 'HTML::FormHandler';
has_field 'name' => ( type => 'Text' );
has_field 'duration' => ( type => 'Duration' );
has_field 'duration.hours' => ( type => 'Nested' );
has_field 'duration.minutes' => ( type => 'Nested' );
}
my $form = Duration::Form->new;
ok( $form, 'get compound form' );
ok( $form->field('duration'), 'duration field' );
ok( $form->field('duration.hours'), 'duration.hours field' );
my $params = { name => 'Testing', 'duration.hours' => 2, 'duration.minutes' => 30 };
$form->process( params => $params );
ok( $form->validated, 'form validated' );
is_deeply($form->fif, $params, 'get fif with right value');
is( $form->field('duration')->value->hours, 2, 'duration value is correct');
$form->process( params => { name => 'Testing', 'duration.hours' => 'abc', 'duration.minutes' => 'xyz' } );
ok( $form->has_errors, 'form does not validate' );
my @errors = $form->errors;
is( $errors[0], 'Invalid value for Duration: Hours', 'correct error message' );
{
package Form::Start;
use HTML::FormHandler::Moose;
extends 'HTML::FormHandler';
has_field 'name' => ( type => 'Text' );
has_field 'start_date' => ( type => 'DateTime' );
has_field 'start_date.month' => ( type => 'Month' );
has_field 'start_date.day' => ( type => 'MonthDay' );
has_field 'start_date.year' => ( type => 'Year' );
sub validate_start_date_month
{
my ( $self, $field ) = @_;
$field->add_error("That month is not available")
if( $field->value == 8 );
}
}
my $dtform = Form::Start->new;
ok( $dtform, 'datetime form' );
my $year = (localtime)[5] + 1900;
$params = { name => 'DT_testing', 'start_date.month' => '10',
'start_date.day' => '2', 'start_date.year' => $year };
$dtform->process( params => $params );
ok( $dtform->validated, 'form validated' );
is( $dtform->field('start_date')->value->mdy, "10-02-$year", 'datetime value');
$params->{'start_date.month'} = 8;
$dtform->process( params => $params );
ok( !$dtform->validated, 'form did not validate' );
ok( $dtform->has_errors, 'form has error' );
@errors = $dtform->errors;
is_deeply( $errors[0], 'That month is not available', 'correct error' );
{
package Field::MyCompound;
use HTML::FormHandler::Moose;
extends 'HTML::FormHandler::Field::Compound';
has_field 'aaa';
has_field 'bbb';
}
{
package Form::TestValues;
use HTML::FormHandler::Moose;
extends 'HTML::FormHandler';
has_field 'compound' => ( type => '+Field::MyCompound', apply => [ { check => sub { $_[0]->{aaa} eq 'aaa'}, message => 'Must be "aaa"' } ] );
}
$form = Form::TestValues->new;
ok( $form, 'Compound form with separate fields declarations created' );
$params = {
'compound.aaa' => 'aaa',
'compound.bbb' => 'bbb',
};
$form->process( params => $params );
is_deeply( $form->values, { compound => { aaa => 'aaa', bbb => 'bbb' } }, 'Compound with separate fields - values in hash' );
is_deeply( $form->fif, $params, 'get fif from compound field' );
$form->process( params => { 'compound.aaa' => undef } );
ok( !$form->field( 'compound' )->has_errors, 'Not required compound with empty sub values is not checked');
{
package Compound;
use HTML::FormHandler::Moose;
extends 'HTML::FormHandler::Field::Compound';
has_field 'year' => (
type => 'Integer',
required => 1,
);
has_field 'month' => (
type => 'Integer',
range_start => 1,
range_end => 12,
);
has_field 'day' => (
type => 'Integer',
range_start => 1,
range_end => 31,
);
sub default {
return {
year => undef,
month => undef,
day => undef
};
}
}
{
package Form;
use HTML::FormHandler::Moose;
extends 'HTML::FormHandler';
has_field 'date' => ( type => '+Compound', required => 1 );
has_field 'foo';
}
my $f = Form->new;
$f->process( { 'date.day' => '18', 'date.month' => '2', 'date.year' => '2010' } );
is_deeply( $f->field('date')->value, { year => 2010, month => 2, day => 18 }, 'correct value' );
$f = Form->new;
$f->process( { foo => 'testing' } );
is_deeply( $f->field('date')->value, { year => undef, month => undef, day => undef }, 'correct default' );
done_testing;
|