thezip has asked for the wisdom of the Perl Monks concerning the following question:
Good monks,
Given the following HTML form, I want to establish validations of the data via Data::FormValidator, and then report the errors with HTML::Template.
I cannot seem to get the third failure mode to work correctly, probably because I'm visualizing the interface incorrectly. Please help.
Here is a simplistic version of the form:
<form method="post"> <input type="text" name="Field1"><br> <input type="text" name="Field2"><br> <input type="submit"> </form>
- Field1 is not a positive integer
- Field2 is not a positive integer
- Field2 is not greater than Field1
sub error_template { return qq( <html> <head> <title>Results</title> </head> <body> <h2>Results</h2> <p>I'm sorry, but I had a slight problem processing your form submissi +on:</p> <ul> <!-- TMPL_IF NAME="err_Field1" --> <li>You must enter a valid number for Field1</li> <!-- /TMPL_IF --> <!-- TMPL_IF NAME="err_Field2" --> <li>You must enter a valid number for Field2</li> <!-- /TMPL_IF --> <!-- TMPL_IF NAME="err_Field2_LT_Field1" --> <li>Field2 must be >= Field1</li> <!-- /TMPL_IF --> </ul> </body> </html> );
In the above template, the conditional for "err_Field2_LT_Field1" doesn't execute during a failure. This is the issue I'm trying to work out.
Now for the Data::FormValidator code:sub compare_num1_LT_num2 { my ($first, $second) = @_; return ($first <= $second); } sub validate { my $data_in = shift; my $maxint = 10000; my $profile = { 'required' => [ qw( Field1 Field2 ) ], 'msgs' => { 'prefix' => 'err_' }, 'constraints' => { 'Field1' => sub { my $in = shift; $in =~ /^\d+$/ && $in > 0 && $in < $maxint }, 'Field2' => sub { my $in = shift; $in =~ /^\d+$/ && $in > 0 && $in < $maxint }, 'Field2_LT_Field1' => { 'constraint' => 'compare_num1_LT_num2', 'params' => [ qw/ Field1 Field2 / ], }, }, }; my $results = Data::FormValidator->check($data_in, $profile); } validate(\%data);
Please forgive me if this code does not compile correctly, as it has been excerpted from a larger bit of code, and is intended for illustrative purposes only.
Thank you in advance for your help.
What can be asserted without proof can be dismissed without proof. - Christopher Hitchens
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: How do I code multiple failure modes for Data::FormValidator field validations?
by trwww (Priest) on Nov 15, 2010 at 23:37 UTC | |
by markjugg (Curate) on Dec 15, 2010 at 01:36 UTC | |
Re: How do I code multiple failure modes for Data::FormValidator field validations?
by ww (Archbishop) on Nov 15, 2010 at 21:46 UTC |