| 1 |
use strict; |
| 2 |
use warnings; |
| 3 |
use Test::More; |
| 4 |
use HTML::FormHandler::Test; |
| 5 |
|
| 6 |
{ |
| 7 |
package MyApp::Test::Form; |
| 8 |
use HTML::FormHandler::Moose; |
| 9 |
extends 'HTML::FormHandler'; |
| 10 |
|
| 11 |
our $labels = { |
| 12 |
foo => 'My Foo', |
| 13 |
bar => 'My Bar', |
| 14 |
}; |
| 15 |
|
| 16 |
has_field 'foo' => ( wrap_label_method => \&wrap_label, |
| 17 |
build_label_method => \&build_label, |
| 18 |
); |
| 19 |
has_field 'bar'; |
| 20 |
sub build_label { |
| 21 |
my $self = shift; # self is field |
| 22 |
my $name = $self->name; |
| 23 |
return $labels->{$name}; |
| 24 |
} |
| 25 |
sub wrap_label { |
| 26 |
my ( $self, $label ) = @_; |
| 27 |
$label ||= $self->label; # so it can be used outside of rendering... |
| 28 |
my $name = $self->name; |
| 29 |
return qq{<a href="/admin/history/$name">$label</a>}; |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
my $form = MyApp::Test::Form->new; |
| 34 |
ok( $form ); |
| 35 |
is( $form->field('foo')->label, 'My Foo', 'label is correct' ); |
| 36 |
is( $form->field('foo')->wrap_label, '<a href="/admin/history/foo">My Foo</a>', |
| 37 |
'wrapped label is correct' ); |
| 38 |
my $rendered = $form->field('foo')->render; |
| 39 |
my $expected = '<div><label for="foo"><a href="/admin/history/foo">My Foo</a></label><input id="foo" name="foo" type="text" value="" /></div>'; |
| 40 |
is_html( $rendered, $expected, 'rendered ok' ); |
| 41 |
|
| 42 |
done_testing; |