Subject: | validate_FIELD method does not run unless field is marked as required |
If you do not set a field as required but want to validate it (for
instance the field is optional but you only want to allow certain
values) and you define a 'validate_' method in the form definition it is
not called.
See attached test that processes form with the only field with required
attribute set to false then true.
Perl version: 5.10.0
OS: Linux 2.6.28-11-generic #42-Ubuntu SMP
Subject: | formhandler_validate.t |
#!/usr/bin/perl
use warnings;
use strict;
use Test::More tests => 2;
{
package TestForm;
use HTML::FormHandler::Moose;
extends 'HTML::FormHandler';
has_field 'foo' => (
label => 'Foo',
type => 'Text',
);
sub validate_foo {
my ($self, $field) = @_;
return $field->add_error('foo failed');
}
no HTML::FormHandler::Moose;
}
for my $required (0, 1) {
my $form = TestForm->new;
# set required flag
$form->field('foo')->required($required);
# process form
$form->process(params => {foo => undef});
my @errors = $form->errors;
ok(
@errors,
'foo has errors',
);
}