Adding a validator to a form element

To add a validator to an element, you will need to open process.php and find the code that creates the element you want to add the validator to. It may already have a line below it that is calling addValidators(), in that case just add the name of the validator you want to add at the end of the array that is passed in. If the element had the following code.

1
$element->addValidator('required');
$element->addValidator('required');

You can just add another line below that adds the new validator. For example if you wanted to add the digits validator also you can make the code:

1
2
$element->addValidator('required');
$element->addValidator('digits');
$element->addValidator('required');
$element->addValidator('digits');

You could also use the following code (note the method name is addValidators instead of addValidator above)

1
$element->addValidators(array('required', 'digits'));
$element->addValidators(array('required', 'digits'));

You can also instantiate the validator directly then add it to the element. Which may help with your own custom validators if you need to make any that you need to pass options to, for example.

1
2
$emailValidator = new Quform_Validator_Email();
$element->addValidator($emailValidator);
$emailValidator = new Quform_Validator_Email();
$element->addValidator($emailValidator);

Still having trouble? Head over to the forums.

Forums