Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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.

What can be asserted without proof can be dismissed without proof. - Christopher Hitchens

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

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others surveying the Monastery: (9)
    As of 2018-03-17 17:31 GMT
    Find Nodes?
      Voting Booth?
      When I think of a mole I think of:

      Results (225 votes). Check out past polls.