Validator options (advanced)

You can pass in options to validators to change the way they operate. You can pass in options using the addValidator() method on an element. Pass in an array as the second argument containing the array keys that are options of the validator. See the documentation below on each validator or the code for the validators in the files in the quform/lib/Quform/Validator/ folder to see the options you can use.

1
$element->addValidator('stringLength', array('min' => 5, 'max' => 10));
$element->addValidator('stringLength', array('min' => 5, 'max' => 10));

You can also pass in options at instantiation and then add the validator object to your element.

1
2
$stringLengthValidator = new Quform_Validator_StringLength(array('min' => 5, 'max' => 10));
$element->addValidator($stringLengthValidator);
$stringLengthValidator = new Quform_Validator_StringLength(array('min' => 5, 'max' => 10));
$element->addValidator($stringLengthValidator);

Alpha validator

Options

  • allowWhiteSpace – boolean true or false, if true white space is allowed, default false

Error messages

KeyDefaultPlaceholders
invalidOnly alphabet characters are allowed%s = the submitted value

Examples

1
$element->addValidator('alpha', array('allowWhiteSpace' => true));
$element->addValidator('alpha', array('allowWhiteSpace' => true));
1
2
3
4
5
$element->addValidator('alpha', array(
    'messages' => array(
        'invalid' => '%s does not contain only alphabet characters'
    )
));
$element->addValidator('alpha', array(
    'messages' => array(
        'invalid' => '%s does not contain only alphabet characters'
    )
));

AlphaNumeric validator

Options

  • allowWhiteSpace – boolean true or false, if true white space is allowed, default false

Error messages

KeyDefaultPlaceholders
invalidOnly alphanumeric characters are allowed%s = the submitted value

Examples

1
$element->addValidator('alphaNumeric', array('allowWhiteSpace' => true));
$element->addValidator('alphaNumeric', array('allowWhiteSpace' => true));
1
2
3
4
5
$element->addValidator('alphaNumeric', array(
    'messages' => array(
        'invalid' => '%s does not contain only alphanumeric characters'
    )
));
$element->addValidator('alphaNumeric', array(
    'messages' => array(
        'invalid' => '%s does not contain only alphanumeric characters'
    )
));

Captcha validator

Options

None

Error messages

KeyDefaultPlaceholders
not_matchThe value does not match 

Examples

1
$element->addValidator('captcha');
$element->addValidator('captcha');
1
2
3
$element->addValidator('captcha', array(
    'messages' => array('not_match' => 'Incorrect captcha solution')
));
$element->addValidator('captcha', array(
    'messages' => array('not_match' => 'Incorrect captcha solution')
));

ChoiceCount validator

This validator validates the number of choices in a Multi Checkbox or Multi Select element.

Options

  • min – (integer) the minimum allowed number of choices
  • max – (integer) the maximum allowed number of choices

Error messages

KeyDefaultPlaceholders
invalidValue is an invalid type, it must be an array 
too_fewPlease select at least %1$s value(s)%1$s = the configured minimum
%2$s = the count of the submitted value
too_manyPlease select no more than %1$s value(s)%1$s = the configured maximum
%2$s = the count of the submitted value

Examples

1
2
3
4
$element->addValidator('choiceCount', array(
    'min' => 1,
    'max' => 3
));
$element->addValidator('choiceCount', array(
    'min' => 1,
    'max' => 3
));
1
2
3
4
5
6
7
8
$element->addValidator('choiceCount', array(
    'min' => 1,
    'max' => 3,
    'messages' => array(
        'too_few' => 'At least %1$s choice is required',
        'too_many' => 'No more than %1$s choices are allowed'
    )
));
$element->addValidator('choiceCount', array(
    'min' => 1,
    'max' => 3,
    'messages' => array(
        'too_few' => 'At least %1$s choice is required',
        'too_many' => 'No more than %1$s choices are allowed'
    )
));

Date validator

Options

None

Error messages

KeyDefaultPlaceholders
invalidThis is not a valid date 

Examples

1
2
3
4
5
$element->addValidator('date', array(
    'messages' => array(
        'invalid' => 'Please enter a valid date'
    )
));
$element->addValidator('date', array(
    'messages' => array(
        'invalid' => 'Please enter a valid date'
    )
));

Digits validator

Options

  • allowWhiteSpace – boolean true or false, if true white space is allowed, default false

Error messages

KeyDefaultPlaceholders
invalidOnly digits are allowed%s = the submitted value

Examples

1
$element->addValidator('digits', array('allowWhiteSpace' => true));
$element->addValidator('digits', array('allowWhiteSpace' => true));
1
2
3
4
5
$element->addValidator('digits', array(
    'messages' => array(
        'invalid' => '%s does not contain only digits'
    )
));
$element->addValidator('digits', array(
    'messages' => array(
        'invalid' => '%s does not contain only digits'
    )
));

Email validator

Options

None

Error messages

KeyDefaultPlaceholders
invalidInvalid email address%s = the submitted value

Examples

1
$element->addValidator('email');
$element->addValidator('email');
1
2
3
$element->addValidator('email', array(
    'messages' => array('invalid' => 'The email address %s is invalid')
));
$element->addValidator('email', array(
    'messages' => array('invalid' => 'The email address %s is invalid')
));

EmailConfirm validator

Options

  • key – (string) the unique name of the other email element, default ’email’

Error messages

KeyDefaultPlaceholders
not_matchThe email addresses do not match%s = the submitted value

Examples

1
$element->addValidator('emailConfirm');
$element->addValidator('emailConfirm');
1
2
3
$element->addValidator('emailConfirm', array(
    'messages' => array('not_match' => 'The email addresses are not the same')
));
$element->addValidator('emailConfirm', array(
    'messages' => array('not_match' => 'The email addresses are not the same')
));

FileUpload validator

The validator is automatically registered with your file upload elements, see the File uploads page for options.

Error messages

KeyDefaultPlaceholders
x_requiredAt least %d files are required%d = the number of required files
one_requiredAt least 1 file is required 
requiredThis field is required 
x_too_big‘%s’ exceeds the maximum allowed file size%s = the filename
too_bigFile exceeds the maximum allowed file size 
x_only_partial‘%s’ was only partially uploaded%s = the filename
only_partialFile was only partially uploaded 
no_fileNo file was uploaded 
missing_temp_folderMissing a temporary folder 
failed_to_writeFailed to write file to disk 
stopped_by_extensionFile upload stopped by extension 
x_bad_typeFile type of ‘%s’ is not allowed%s = the filename
bad_typeFile type is not allowed 
x_not_uploadedFile ‘%s’ is not an uploaded file%s = the filename
not_uploadedFile is not an uploaded file 
unknownUnknown upload error 

Examples

1
2
3
4
$upload->getFileUploadValidator()->setMessageTemplates(array(
    'required' => 'Please upload a file',
    'unknown' => 'Upload failed, please try again'
));
$upload->getFileUploadValidator()->setMessageTemplates(array(
    'required' => 'Please upload a file',
    'unknown' => 'Upload failed, please try again'
));

GreaterThan validator

Options

  • min – (integer|string) the value must be greater than min (numerically)

Error messages

KeyDefaultPlaceholders
not_numericValue is not numeric%1$s = the submitted value
%2$d = the set minimum
not_greater_than%1$s is not greater than %2$d%1$s = the submitted value
%2$d = the set minimum

Examples

1
$element->addValidator('greaterThan', array('min' => 3));
$element->addValidator('greaterThan', array('min' => 3));
1
2
3
4
5
6
$element->addValidator('greaterThan', array(
    'messages' => array(
        'not_numeric' => 'Please enter a number',
        'not_greater_than' => '%1$s is not greater than %2$d, please change the value'
    )
));
$element->addValidator('greaterThan', array(
    'messages' => array(
        'not_numeric' => 'Please enter a number',
        'not_greater_than' => '%1$s is not greater than %2$d, please change the value'
    )
));

Honeypot validator

Options

None

Error messages

KeyDefaultPlaceholders
emptyThis field should be empty 

Examples

1
$element->addValidator('honeypot');
$element->addValidator('honeypot');
1
2
3
4
5
$element->addValidator('honeypot', array(
    'messages' => array(
        'empty' => 'Leave this field blank'
    )
));
$element->addValidator('honeypot', array(
    'messages' => array(
        'empty' => 'Leave this field blank'
    )
));

Identical validator

Options

  • token – (mixed) the token to compare the value against

Error messages

KeyDefaultPlaceholders
not_matchValue does not match%1$s = the submitted value
%2$s = the set token

Examples

1
$element->addValidator('identical', array('token' => 'light'));
$element->addValidator('identical', array('token' => 'light'));
1
2
3
4
5
$element->addValidator('identical', array(
    'messages' => array(
        'not_match' => '%1$s does not match %2$s'
    )
));
$element->addValidator('identical', array(
    'messages' => array(
        'not_match' => '%1$s does not match %2$s'
    )
));

InArray validator

Options

  • haystack – (array) the array of possible values
  • strict – (boolean) whether to check data type as well as value, default true
  • invert – (boolean) whether to invert the check (i.e. value must not be in the array)

Error messages

KeyDefaultPlaceholders
not_in_arrayThe given value is not valid 

Examples

1
2
3
4
5
6
7
8
$element->addValidator('inArray', array(
    'haystack' => array(
        'Value 1', 'Value 2', 'Value 3'
    ),
    'messages' => array(
        'not_in_array' => 'This value is not allowed'
    )
));
$element->addValidator('inArray', array(
    'haystack' => array(
        'Value 1', 'Value 2', 'Value 3'
    ),
    'messages' => array(
        'not_in_array' => 'This value is not allowed'
    )
));

LessThan validator

Options

  • max – (integer|string) the value must be less than max (numerically)

Error messages

KeyDefaultPlaceholders
not_less_than%1$s is not less than %2$d%1$s = the submitted value
%2$d = the set maximum

Examples

1
$element->addValidator('lessThan', array('max' => 10));
$element->addValidator('lessThan', array('max' => 10));
1
2
3
4
5
$element->addValidator('lessThan', array(
    'messages' => array(
        'not_less_than' => '%1$s is not less than %2$d, please change the value'
    )
));
$element->addValidator('lessThan', array(
    'messages' => array(
        'not_less_than' => '%1$s is not less than %2$d, please change the value'
    )
));

reCAPTCHA validator

Options

  • secretKey – (string) the reCAPTCHA secret key

Error messages

KeyDefaultPlaceholders
missing-input-secretThe secret parameter is missing 
invalid-input-secretThe secret parameter is invalid or malformed 
missing-input-responseThe response parameter is missing 
invalid-input-responseThe response parameter is invalid or malformed 
errorAn error occurred, please try again 

Examples

1
$element->addValidator('recaptcha');
$element->addValidator('recaptcha');
1
2
3
$element->addValidator('captcha', array(
    'messages' => array('invalid-input-secret' => 'The reCAPTCHA was invalid')
));
$element->addValidator('captcha', array(
    'messages' => array('invalid-input-secret' => 'The reCAPTCHA was invalid')
));

Regex validator

Options

  • pattern – (string) the regex pattern

Error messages

KeyDefaultPlaceholders
invalidInvalid type given. String, integer or float expected 
not_matchThe given value is not valid%s = the given value

Examples

1
2
3
4
5
6
$element->addValidator('regex', array(
    'pattern' => '/\d+/',
    'messages' => array(
        'not_match' => 'The given value "%s" is not a valid number'
    )
));
$element->addValidator('regex', array(
    'pattern' => '/\d+/',
    'messages' => array(
        'not_match' => 'The given value "%s" is not a valid number'
    )
));

Required validator

Options

None

Error messages

KeyDefaultPlaceholders
requiredThis field is required 

Examples

1
$element->addValidator('required');
$element->addValidator('required');
1
2
3
$element->addValidator('required', array(
    'messages' => array('required' => 'Please fill out this field')
));
$element->addValidator('required', array(
    'messages' => array('required' => 'Please fill out this field')
));

StringLength validator

Options

  • min – (integer) the minumum length of the string
  • max – (integer) the maximum length of the string, if not specified there is no maximum

Error messages

KeyDefaultPlaceholders
invalidValue is an invalid type, it must be a string 
too_shortValue must be at least %1$d characters long%1$d = the set minimum
%2$s = the submitted value
%3$d = the length of the submitted value
too_longValue must be no longer than %1$d characters%1$d = the set maximum
%2$s = the submitted value
%3$d = the length of the submitted value

Examples

1
$element->addValidator('stringLength', array('min' => 5, 'max' => 10));
$element->addValidator('stringLength', array('min' => 5, 'max' => 10));
1
2
3
4
5
6
7
8
9
$element->addValidator('stringLength', array(
    'min' => 5,
    'max' => 10,
    'messages' => array(
        'invalid' => 'Sorry, the value must be a string',
        'too_short' => 'Your submitted value %2$s must be at least %1$d characters long, but it was %3$d',
        'too_long' => 'Your submitted value %2$s must be no longer than %1$d characters, but it was %3$d'
    )
));
$element->addValidator('stringLength', array(
    'min' => 5,
    'max' => 10,
    'messages' => array(
        'invalid' => 'Sorry, the value must be a string',
        'too_short' => 'Your submitted value %2$s must be at least %1$d characters long, but it was %3$d',
        'too_long' => 'Your submitted value %2$s must be no longer than %1$d characters, but it was %3$d'
    )
));

Time validator

Options

None

Error messages

KeyDefaultPlaceholders
invalidThis is not a valid time 

Examples

1
2
3
4
5
$element->addValidator('time', array(
    'messages' => array(
        'invalid' => 'Please enter a valid time'
    )
));
$element->addValidator('time', array(
    'messages' => array(
        'invalid' => 'Please enter a valid time'
    )
));

WordCount validator

Options

  • min – (integer) the minumum number of words
  • max – (integer) the maximum number of words, if not specified there is no maximum

Error messages

KeyDefaultPlaceholders
invalidValue is an invalid type, it must be a string 
too_shortValue must contain at least %1$d words%1$d = the set minimum
%2$s = the given value
%3$d = the number of words in the given value
too_longValue must contain no more than %1$d words%1$d = the set maximum
%2$s = the given value
%3$d = the number of words in the given value

Examples

1
2
3
4
5
6
7
8
$element->addValidator('wordCount', array(
    'min' => 10,
    'max' => 100
    'messages' => array(
        'too_short' => 'Please enter more than %1$d words',
        'too_long' => 'Please enter no more than %1$d words, you entered %3$d words'
    )
));
$element->addValidator('wordCount', array(
    'min' => 10,
    'max' => 100
    'messages' => array(
        'too_short' => 'Please enter more than %1$d words',
        'too_long' => 'Please enter no more than %1$d words, you entered %3$d words'
    )
));
Be inspired. © 2024 ThemeCatcher Ltd. 20-22 Wenlock Road, London, England, N1 7GU | Company No. 08120384 | Built with React | Privacy Policy