void setRule(
array
$rules)
|
|
Sets validation rules for the control
Here are the available rules:
Specified as 'mandatory' => array($errorMessageContainerBlock, $errorMessage)
Validates only if the control has a value
Available for HTMLForm_checkbox, HTMLForm_password, HTMLForm_radio, HTMLForm_select, HTMLForm_text, HTMLForm_textarea
$obj->setRule('mandatory' =>
array('errorBlock1', 'This field is required'));
Specified as 'minlength' => array($minimumLength, $errorMessageContainerBlock, $errorMessage)
Validates only if entered text's length is greater than $minimumLength
Available for HTMLForm_password, HTMLForm_text, HTMLForm_textarea
$obj->setRule('minlength' =>
array('6', 'errorBlock1', '6 characters is minimum!'));
Specified as 'maxlength' => array($maximumLength, $errorMessageContainerBlock, $errorMessage)
Validates only if entered text's length is shorter than $maximumLength
Available for HTMLForm_password, HTMLForm_text, HTMLForm_textarea
$obj->setRule('maxlength' =>
array('12', 'errorBlock1', '12 Characters is maximum', ));
Specified as 'email' => array($errorMessageContainerBlock, $errorMessage)
Validates only if entered text is a valid email address
Available for HTMLForm_password, HTMLForm_text, HTMLForm_textarea
$obj->setRule('email' =>
array('errorBlock1', 'Not a valid email address!'));
Specified as 'emails' => array($errorMessageContainerBlock, $errorMessage)
Validates only if entered text is a valid list of comma separated email addresses
Available for HTMLForm_password, HTMLForm_text, HTMLForm_textarea
$obj->setRule('emails' =>
array('errorBlock1', 'Invalid email address/addresses!'));
Specified as 'digitsonly' => array($errorMessageContainerBlock, $errorMessage)
Validates only if entered characters are all digits
Available for HTMLForm_password, HTMLForm_text, HTMLForm_textarea
$obj->setRule('digitsonly' =>
array('errorBlock1', 'Only numbers allowed!'));
Specified as 'compare' => array($controlIDToCompareWith, $errorMessageContainerBlock, $errorMessage)
Validates only if control's value is equal with the value of the control indicated by $controlIDToCompareWith
Useful for when you want to check password confirmation
Available for HTMLForm_password, HTMLForm_text, HTMLForm_textarea
$obj->setRule('compare' =>
array('password', 'errorBlock1', 'Password not confirmed correctly!'));
Specified as 'captcha' => array($errorMessageContainerBlock, $errorMessage)
Validates only if control's value is the same as the characters seen in the HTMLForm_captcha control on the form
...therefore, you must also have a HTMLForm_captcha control on your form!
Available for HTMLForm_text
$obj->setRule('captcha' =>
array('errorBlock1', 'Password not confirmed correctly!'));
This rule allows you to define custom rules.
Custom rules allow you to do customized validations within the $form->validate() method (as opposed to doing your custom validations after the $form->validate() method and thus giving feedback to the user only after validating for standard rules which would be awkward for the user because it will look like after passing all validation rules he will get error messages that did not showed up earlier)
It must be specified as 'custom' => array($callbackFunctionName, [optional arguments to be passed to the function], $errorMessageContainerBlock, $errorMessage)
Note that the custom function's first parameter will ALWAYS be the control's submitted value and the optional arguments
to be passed to the function will start as of second argument!
Also note that the custom validation function MUST return TRUE on success or FALSE on failure!
Note that multiple custom rules can also be set:
It must be specified as
'custom' => array(
array($callbackFunctionName1, [optional arguments to be passed to the function], $errorMessageContainerBlock, $errorMessage), array($callbackFunctionName2, [optional arguments to be passed to the function], $errorMessageContainerBlock, $errorMessage)
)
/*
custom function that checks if a control's value is equal
to a defined value
*/
function textIs($controlsSubmittedValue, $valueToCompareTo)
{
if ($controlsSubmittedValue != $valueToCompareTo) {
return false;
}
return true;
}
/*
add a text box control to the form
*/
$obj = & $form->add('text', 'control_id');
/*
set the custom rule which will compare weather the text box control's submitted value
is 'admin' and display the error message in specified error block if is not
*/
$obj->setRule('custom' =>
array('textIs', 'admin', 'errorBlock', 'Text must be 'admin'!'));
Parameters: