Form Validation using SmartyValidate
Form validation is one of the most frequently performed tasks when designing websites and developers must try to find the easiest way to do it. What we are looking for is to validate a form before the data is sent to the server, in order to save time going back and forth to the server with bad data. We want to validate as much as possible the data that is sent to the server, before it is sent! SmartyValidate abstracts the validation process and simplifies the chore to the programmer. He only has to provide the validation criteria and the error message, and SmartyValidate does the rest!
Here is how we use SmartyValidate:
First we call the SmartyValidate connect() function passing the smarty object as a parameter: SmartyValidate::connect($smarty). Then, we register the validators with the SamrtyValidate::register_validator() function, once for each field we want to validate in the form.
When the form is posted, we call SmartyValidate::is_valid($_POST) and depending on the outcome, the form is displayed again to correct the errors or sent to the server if there are no errors. The error message displayed will be the one that was specified in the {validate...} tag on the template. We, therefore, choose the error message we want to display.
Features
SmartyValidate supplies a number of most common validations like integer, float, email, valid dates, ranges, empty field, credit card expiration dates and much more. We can also add our own validation functions or add transform functions that can be applied to form values before they are validated. Multiple validations can be applied to the same field, but once one validation fails, the remaining validations will be ignored. Let's look at an example to see how SmartyValidate works. Let's assume that we are designing a CMS for a realtor website, and that we want to show the property codes for a realtor name, last name and code. We will have three boxes where the realtor can input these three values, but we will not implement the display of the property codes, since it's of no use for the purpose of this example. First the template form: The first row displays the names of the boxes, the second row displays the error messages if there are any, and the last row displays the input boxes.
property_codes.tpl
<form name="myform" action="index.php?Page=Property_Codes" method="post">
<table align="center" cellpadding="3" cellspacing="1" border="0" width="100%">
<tr class="TableHeader">
<td align="center" colspan="4">Property Codes</td>
</tr>
<tr>
<td>Realtor First Name</td>
<td>Realtor Last Name</td>
<td>Realtor Code</td>
</tr>
<tr>
<td class="error">
{validate id="first_name" message="First Name cannot be empty"}
First Name: <input type="text" name="ifirstname" value="{$FirstName|escape}"><br />
</td>
<td class="error">
{validate id="last_name" message="Last Name cannot be empty"}
Last Name: <input type="text" name="ilastname" value="{$LastName|escape}"><br />
</td>
<td class="error">
{validate id="code" message="Code must be a number"}
Code: <input type="text" name="icode" value="{$Code|escape}"><br />
</td>
</tr>
<tr>
<td><input type="submit" name="submit_prop_code" class="SubmitButton" value="Search"/>
</td>
</tr>
</table>
</form>
The property_code.php file will include the Smarty validators corresponding to the template form and the code that will display the error messages or send the data to the server.
property_code.php
<?php
//SmartyValidate
if(empty($_POST))
{
SmartyValidate::connect($page, true);
SmartyValidate::register_validator('first_name', 'ifirstname,'notEmpty');
SmartyValidate::register_validator('last_name', 'ilastname','notEmpty');
SmartyValidate::register_validator('code', 'icode','notEmpty');
//display form
$pageContent = "property_codes.tpl";
}
else
{
SmartyValidate::connect($page);
// validate after a POST
if(SmartyValidate::is_valid($_POST)) {
//no errors, redraw the form
SmartyValidate::disconnect();
$smarty->display('success.tpl');
} else
{
// error, redraw the form
$smarty->assign($_POST);
$smarty->display('property_codes.tpl');
}
}
?>







