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
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.