Most dynamic web application uses forms to let the user enter some data. Because the
user makes errors while filling out the form it's necessary to validate them. Embperl has a module called Embperl::Form::Validate, which does this job for you.
It works
on the server side by checking the posted form data and it
generates client side script functions, to validate the
form values, as far as possible, before they are send to
the server, to avoid another server roundtrip. It can be extended by new validation rules for
additional syntaxes (e.g. US zip codes, German
Postleitzahlen, number plates, iso-3166 2-digit language or country
codes, etc.) Each module has the ability to rely it's answer on parameters like
e.g. the browser, which caused the request for or submitted the form. The module fully supports internationalisation. Any message can be
provided in multiple languages and it makes use of Embperl's
multilanguage support. Let's look at an example. The login form of the Embperl website contains
the following code: [-
$epf1 = new Embperl::Form::Validate([ -key => 'user_email',
required => 1,
-key => 'user_password',
required => 1,
length_min => 5],
'login');
-]
<script>
[+ do { local $escmode = 0 ; $epf1 -> get_script_code } +]
</script> <form action="[+ $param[0] +]" method="POST" name="login" onSubmit="return epform_validate_login()">
<table>
<tr>
<td class="cText">[= user_email =]</td>
<td class="cInput"><input type="text" name="user_email"></td>
</tr>
<tr>
<td class="cText">[= user_password =]</td>
<td class="cInput"><input type="password" name="user_password"></td>
</tr>
</table>
<p>
<input type="submit" name="-login" value="[= login =]">
</p>
</form> It first creates a Embperl::Form::Validate object, which get's passed some rules and
the name of the form, which it should validate. Below we add some script code that is
created by Embperl::Form::Validate and add a onSubmit handler to the form tag, to
verify that the input conforms to our rules, when the user hits the submit button. The rules are an array. All rules are processed in the order given. First you have to
name the formfield which should be validated, then you can give the rules it should conform to.
For the email address we tell Embperl::Form::Validate that it is a required field, for the
password we addtionaly say that it has to be at least five characters long.
There are a lot more possibilies that can be used to validate the form and you can modify
message and names that Embperl display in the error message. If you do't give a name of
fields which should presented to the user, Embperl takes the -key argument and tries to
lookup the correct text to display for this name from the Embperl internationalization feature.
So in the case of the Embperl website we already have defined message ids for user_email
and user_password, so the error message will contain the fieldnames in the correct language.
Since the rest of error message also depend on the language defined by Embperl the whole
error message is internationalizied. Embperl::Form::Validate already has build in support
english and german error messages, but it's up to you to translate them in your favorite
language. Of course browser side form validation is a nice feature, but the user can turn of
JavaScript and nothing will be happen. So we need an addtional server side form validation.
Embperl::Form::Validate does this when you call validate_messages . It returns an
array ref with all error message or an array ref to an empty error in case everything is ok.
It's up to you to display the message to the user and take the correct action. If Embperl::Form::Validate doesn't already have the sort of validation your application
needs, you can extent it by writing a new class and derive it from Embperl::Form::Validate::Default.
|