| 1 |
use strict; |
| 2 |
use warnings; |
| 3 |
use Test::More; |
| 4 |
use HTML::FormHandler::Test; |
| 5 |
|
| 6 |
{ |
| 7 |
package MyApp::Form::Theme::Basic; |
| 8 |
use Moose::Role; |
| 9 |
|
| 10 |
sub build_do_form_wrapper {1} |
| 11 |
sub build_form_tags {{ wrapper_tag => 'div' }} |
| 12 |
sub build_update_subfields {{ |
| 13 |
all => { tags => { label_tag => 'span' } }, |
| 14 |
by_flag => { 'compound' => { do_wrapper => 1, do_label => 1, |
| 15 |
tags => { wrapper_tag => 'span' }}}, |
| 16 |
}} |
| 17 |
sub html_attributes { |
| 18 |
my ( $self, $field, $type, $attr ) = @_; |
| 19 |
$attr->{class} = ['frm', 'ele'] if $type eq 'element'; |
| 20 |
$attr->{class} = ['frm', 'lbl'] if $type eq 'label'; |
| 21 |
$attr->{class} = ['frm', 'wrp'] if $type eq 'wrapper'; |
| 22 |
return $attr; |
| 23 |
} |
| 24 |
} |
| 25 |
{ |
| 26 |
package MyApp::Form; |
| 27 |
use HTML::FormHandler::Moose; |
| 28 |
extends 'HTML::FormHandler'; |
| 29 |
with 'MyApp::Form::Theme::Basic'; |
| 30 |
|
| 31 |
has_field 'my_comp' => ( type => 'Compound' ); |
| 32 |
has_field 'my_comp.one'; |
| 33 |
has_field 'my_comp.two'; |
| 34 |
has_field 'my_text' => ( type => 'Text' ); |
| 35 |
} |
| 36 |
|
| 37 |
my $form = MyApp::Form->new; |
| 38 |
my $rendered = $form->field('my_comp')->render; |
| 39 |
my $expected = |
| 40 |
'<span class="frm wrp" id="my_comp"> |
| 41 |
<span class="frm lbl">My comp</span> |
| 42 |
<div class="frm wrp"> |
| 43 |
<span class="frm lbl">One</span> |
| 44 |
<input class="frm ele" type="text" name="my_comp.one" id="my_comp.one" value="" /> |
| 45 |
</div> |
| 46 |
<div class="frm wrp"> |
| 47 |
<span class="frm lbl">Two</span> |
| 48 |
<input class="frm ele" type="text" name="my_comp.two" id="my_comp.two" value="" /> |
| 49 |
</div> |
| 50 |
</span>'; |
| 51 |
is_html( $rendered, $expected, 'compound rendered ok' ); |
| 52 |
|
| 53 |
{ |
| 54 |
package Test::DT; |
| 55 |
use HTML::FormHandler::Moose; |
| 56 |
extends 'HTML::FormHandler'; |
| 57 |
|
| 58 |
sub build_update_subfields {{ all => { tags => { label_after => ': ' }}}} |
| 59 |
has_field 'start_date' => ( type => 'DateTime', do_wrapper => 1, do_label => 1, |
| 60 |
tags => { wrapper_tag => 'fieldset' }, wrapper_class => 'start_date' ); |
| 61 |
has_field 'start_date.month' => ( type => 'Integer', range_start => 1, |
| 62 |
range_end => 12 ); |
| 63 |
has_field 'start_date.day' => ( type => 'Integer', range_start => 1, |
| 64 |
range_end => 31 ); |
| 65 |
has_field 'start_date.year' => ( type => 'Integer', range_start => 2000, |
| 66 |
range_end => 2020 ); |
| 67 |
} |
| 68 |
|
| 69 |
$form = Test::DT->new; |
| 70 |
my $params = { 'start_date.month' => 7, 'start_date.day' => 14, 'start_date.year' => '2006' }; |
| 71 |
$form->process( $params ); |
| 72 |
|
| 73 |
$rendered = $form->field('start_date')->render; |
| 74 |
is_html( $rendered, |
| 75 |
'<fieldset class="start_date" id="start_date"><legend>Start date</legend> |
| 76 |
<div> |
| 77 |
<label for="start_date.month">Month: </label> |
| 78 |
<input type="text" name="start_date.month" id="start_date.month" size="8" value="7" /> |
| 79 |
</div> |
| 80 |
<div> |
| 81 |
<label for="start_date.day">Day: </label> |
| 82 |
<input type="text" name="start_date.day" id="start_date.day" size="8" value="14" /> |
| 83 |
</div> |
| 84 |
<div> |
| 85 |
<label for="start_date.year">Year: </label> |
| 86 |
<input type="text" name="start_date.year" id="start_date.year" size="8" value="2006" /> |
| 87 |
</div> |
| 88 |
</fieldset>', |
| 89 |
'output from DateTime' ); |
| 90 |
|
| 91 |
done_testing; |