| 1 |
use strict; |
| 2 |
use warnings; |
| 3 |
use Test::More; |
| 4 |
|
| 5 |
|
| 6 |
my $struct = { |
| 7 |
username => 'Joe Blow', |
| 8 |
occupation => 'Programmer', |
| 9 |
tags => ['Perl', 'programming', 'Moose' ], |
| 10 |
employer => { |
| 11 |
name => 'TechTronix', |
| 12 |
country => 'Utopia', |
| 13 |
}, |
| 14 |
}; |
| 15 |
|
| 16 |
|
| 17 |
{ |
| 18 |
package Structured::Form; |
| 19 |
use HTML::FormHandler::Moose; |
| 20 |
extends 'HTML::FormHandler'; |
| 21 |
|
| 22 |
has_field 'username'; |
| 23 |
has_field 'occupation'; |
| 24 |
has_field 'tags' => ( type => 'Repeatable' ); |
| 25 |
has_field 'tags.contains' => ( type => 'Text' ); |
| 26 |
has_field 'employer' => ( type => 'Compound' ); |
| 27 |
has_field 'employer.name'; |
| 28 |
has_field 'employer.country'; |
| 29 |
|
| 30 |
} |
| 31 |
|
| 32 |
my $form = Structured::Form->new; |
| 33 |
ok( $form, 'form created' ); |
| 34 |
$form->process( params => $struct ); |
| 35 |
ok( $form->validated, 'form validated'); |
| 36 |
|
| 37 |
is_deeply( $form->field('tags')->value, ['Perl', 'programming', 'Moose' ], |
| 38 |
'list field tags has right values' ); |
| 39 |
my $result = $form->result; |
| 40 |
is( $result->num_results, 4, 'right number of results'); |
| 41 |
|
| 42 |
my $first_tag = $result->field('tags.0'); |
| 43 |
is( ref $first_tag, 'HTML::FormHandler::Field::Result', 'get result object'); |
| 44 |
is( $first_tag->value, 'Perl', 'result has right value' ); |
| 45 |
is( $first_tag->parent, $result->field('tags'), 'correct parent for '); |
| 46 |
|
| 47 |
my $employer = $result->field('employer'); |
| 48 |
my $employer_name = $result->field('employer.name'); |
| 49 |
is( $employer_name->parent, $employer, 'correct parent for compound field'); |
| 50 |
|
| 51 |
my $fif = { |
| 52 |
'employer.country' => 'Utopia', |
| 53 |
'employer.name' => 'TechTronix', |
| 54 |
'occupation' => 'Programmer', |
| 55 |
'tags.0' => 'Perl', |
| 56 |
'tags.1' => 'programming', |
| 57 |
'tags.2' => 'Moose', |
| 58 |
'username' => 'Joe Blow' |
| 59 |
}; |
| 60 |
|
| 61 |
is_deeply( $form->fif, $fif, 'fif is correct' ); |
| 62 |
|
| 63 |
$result = $form->run( $fif ); |
| 64 |
|
| 65 |
ok( $result->validated, 'form processed from fif' ); |
| 66 |
|
| 67 |
is_deeply( $result->value, $struct, 'values round-tripped from fif'); |
| 68 |
|
| 69 |
done_testing; |