Beefy Boxes and Bandwidth Generously Provided by pair Networks vroom
Problems? Is your data what you think it is?

Comment on


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>
Let's say that I want to have three failures modes:
  • Field1 is not a positive integer
  • Field2 is not a positive integer
  • Field2 is not greater than Field1
Here is the template:
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.


In reply to How do I code multiple failure modes for Data::FormValidator field validations? by thezip

and:  <code> code here </code>

  • Please read these before you post! —
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others surveying the Monastery: (14)
    As of 2014-03-14 14:55 GMT
    Find Nodes?
      Voting Booth?

      Have you used a cryptocurrency?

      Results (307 votes), past polls