Skip Menu |
 

Preferred bug tracker

Please visit the preferred bug tracker to report your issue.

This queue is for tickets about the HTML-FormHandler CPAN distribution.

Report information
The Basics
Id: 47145
Status: resolved
Worked: 2 hours (120 min)
Priority: 0/
Queue: HTML-FormHandler

People
Owner: gshank [...] cpan.org
Requestors: pete [...] cubabit.net
Cc:
AdminCc:

Bug Information
Severity: Important
Broken in: 0.23
Fixed in: (no value)



Subject: validate_FIELD method does not run unless field is marked as required
Download (untitled) / with headers
text/plain 390b
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', ); }
This has been fixed in 0.24.
Download (untitled) / with headers
text/plain 232b
This has not been fixed in 0.24. This is by design. The 'validate_<field_name>' methods are not called when the value is undef, but the fact that the method IS called for required fields seems odd. Will look into this behavior.
Download (untitled) / with headers
text/plain 587b
In the testcase the validate_foo routine was not being called for either required=1 or required=0. The error that occurred when the field was required was the required error, not the 'validate_foo' error. If you want an error message when the field is empty, then use 'required' and 'required_message'. Other alternatives are using the form's 'validate' method which is always called, or using 'validate' in a custom field class. Logically it's not necessary to be able to issue an error when the field is empty and the field is not required. That's what the required flag is for...
From: pete [...] cubabit.net
Download (untitled) / with headers
text/plain 1.1k
On Sat Jun 20 17:13:46 2009, GSHANK wrote: Show quoted text
> In the testcase the validate_foo routine was not being called for > either required=1 > or required=0. The error that occurred when the field was required was > the > required error, not the 'validate_foo' error. > > If you want an error message when the field is empty, then use > 'required' and > 'required_message'. Other alternatives are using the form's 'validate' > method > which is always called, or using 'validate' in a custom field class. > > Logically it's not necessary to be able to issue an error when the > field is empty and > the field is not required. That's what the required flag is for...
I understand why you rejected this. But I hope you can answer this (sorry if this is not the right place). What I want to do is make a text field (field A) required when another select field (field B) is set to a particular option (this is an example). What I was planning to do was check in the validate method for field A the value of field B, and if it is set to the particular option, return an error if field A is blank. But of course field A's validation method does not get called if field A is blank. How can I do this?
Download (untitled) / with headers
text/plain 1.2k
On Wed Jun 24 18:14:34 2009, cubabit-2 wrote: Show quoted text
> >
> I understand why you rejected this. But I hope you can answer this > (sorry if this is not the right place). What I want to do is make a text > field (field A) required when another select field (field B) is set to a > particular option (this is an example). > > What I was planning to do was check in the validate method for field A > the value of field B, and if it is set to the particular option, return > an error if field A is blank. > > But of course field A's validation method does not get called if field A > is blank. > > How can I do this? >
There are a number of places that you could do this, but for your particular situation the best is probably in the form's validate method. (it used to be called cross_validate). package MyApp::Form::User; use HTML::FormHandler::Moose; extends 'HTML::FormHandler'; has_field 'field_one' => ( required => 1 ); has_field 'dependent_field'; sub validate { my $self = shift; $self->field('dependent_field')->add_error("You must ..." ) if( $self->field('field_one')->value eq 'something' && !$self->field('dependent_field')->value ); } 1; There's a FormHandler email list at http://groups.google.com/group/formhandler .
Closing.


This service is sponsored and maintained by Best Practical Solutions and runs on Perl.org infrastructure.

Please report any issues with rt.cpan.org to rt-cpan-admin@bestpractical.com.