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

Key Default Placeholders
invalid Only 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

Key Default Placeholders
invalid Only 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

Key Default Placeholders
not_match The 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')
));

Date validator

Options

None

Error messages

Key Default Placeholders
invalid This 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

Key Default Placeholders
invalid Only 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

Key Default Placeholders
invalid Invalid 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

Key Default Placeholders
not_match The 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

Key Default Placeholders
x_required At least %d files are required %d = the number of required files
one_required At least 1 file is required  
required This field is required  
x_too_big ‘%s’ exceeds the maximum allowed file size %s = the filename
too_big File exceeds the maximum allowed file size  
x_only_partial ‘%s’ was only partially uploaded %s = the filename
only_partial File was only partially uploaded  
no_file No file was uploaded  
missing_temp_folder Missing a temporary folder  
failed_to_write Failed to write file to disk  
stopped_by_extension File upload stopped by extension  
x_bad_type File type of ‘%s’ is not allowed %s = the filename
bad_type File type is not allowed  
x_not_uploaded File ‘%s’ is not an uploaded file %s = the filename
not_uploaded File is not an uploaded file  
unknown Unknown 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

Key Default Placeholders
not_numeric Value 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

Key Default Placeholders
empty This 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

Key Default Placeholders
not_match Value 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

Error messages

Key Default Placeholders
not_in_array The 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

Key Default Placeholders
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

Key Default Placeholders
missing-input-secret The secret parameter is missing  
invalid-input-secret The secret parameter is invalid or malformed  
missing-input-response The response parameter is missing  
invalid-input-response The response parameter is invalid or malformed  
error An 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

Key Default Placeholders
invalid Invalid type given. String, integer or float expected  
not_match The 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

Key Default Placeholders
required This 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

Key Default Placeholders
invalid Value is an invalid type, it must be a string  
too_short Value 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_long Value 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

Key Default Placeholders
invalid This 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

Key Default Placeholders
invalid Value is an invalid type, it must be a string  
too_short Value 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_long Value 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'
    )
));

Still having trouble? Head over to the forums.

Forums