Saturday, October 2, 2010

validation

Custom Validator Sample

This sample demonstrates how to use Custom Validator for both server- and client-side validation.
Run Custom Validator Sample
In this sample my custom validator checks your credit card expiration date. Firstly, look to the validator:
<vlvalidator name="ExpDate" type="custom" errmsg="Something wrong" function="phpExpDateCheck" clientfunction="jsExpDateCheck">
Note that my validator doesn't have control attribute. If you add controll attribute to your custom validator you will get this control value from the parameter of custom validation function. In this example I will validate two input fields, so, anyway I need to get values by myself.

Client-side Validation

As you can see client-side validation function name is jsExpDateCheck. Client-side validation function must be javascript function defined on the same page as the validated form. It should have the following prototype:
void validationFunctionName(oStatus)
where oStatus parameter is javascript object with the following properties:
isvalid - you must set here result of validation. If you do not set it, validator evaluated to true value - validation passed.
errmsg - used to customize error message, will be displayed in the Summary.
value - contains current value of input element being validated. If input element (control attribute) is not defined it contains null.
Now look at my jsExpDateCheck function (find it in the code bellow). It fully conforms to prototype - have one and only one parameter. My code retrieves current control values using HTML DOM:
year = document.forms["Custom"].elements["ExpYear"].value;
month = document.forms["Custom"].elements["ExpMonth"].value;

Then my code decides is this data valid or not. If valid then I do not need to do anything. Invalid status can be set this way:
oStatus.isvalid = false;
oStatus.errmsg = "The credit date entered is not valid";

Client-side validation is optional and very often can't be implemented (when you need access to database for example). In most cases you will not write client-side custom validation function.

Server-side Validation

You must always implement server-side validation for a custom validator. Server-side validation function must be php function defined on the action page (page specified in the form action attribute). This function should have the following prototype:
void validationFunctionName($sValue, &$oStatus)
where string parameter $sValue contains value of input element being validated. If input element is not defined it contains null.
$oStatus is object of CVDValidatorStatus type (declared in vdaemon.php file). It has the following properties:
bValid - you must set here result of validation. If you do not set it, validator evaluated to true value.
sErrMsg - used to customize error message, will be displayed in the Summary.
My server-side custom validation function name is phpExpDateCheck (again find it bellow in the code of custom_p.php file). It has exactly two parameters (second parameter passed by reference) as prototype requires. My code gets values for validation from $_POST array:
$nMonth = $_POST['ExpMonth'];
$nYear = $_POST['ExpYear'];

and then decides is this data valid or not. If valid then I do not need to do anything. Invalid status can be set this way:
$oStatus->bValid = false;
$oStatus->sErrMsg = "The credit date entered is not valid";

Conclusion: it is not recomended to do any output from custom validation function (echo, print, etc).
Run Custom Validator Sample

No comments:

Post a Comment