Creating a custom validator

The Quform WordPress v2 documentation is not yet finished. Please post on the forums if you have any questions in the mean time.

Click here to visit the complete documentation for Quform 1.x

Add the following code to your theme functions.php file (or create a plugin for it) and customize it to suit. If the given value $value fails your own validation, then set $valid to false and set the error message by calling $element->setError('Your error message');. See the examples below.

Require that the value is equal to a string

1
2
345
6
7
8
9
10
function my_custom_validator($valid, $value, Quform_Element_Field $element)
{
    if ($value != 'Foo') {        $element->setError('The value is not Foo');        $valid = false;
    }
 
    return $valid;
}
add_filter('quform_element_valid_1_1', 'my_custom_validator', 10, 3);
function my_custom_validator($valid, $value, Quform_Element_Field $element)
{
    if ($value != 'Foo') {
        $element->setError('The value is not Foo');
        $valid = false;
    }

    return $valid;
}
add_filter('quform_element_valid_1_1', 'my_custom_validator', 10, 3);

Modify the code to suit the form setup and add your own validation code.

  • On line 3, change the the test to suit your own validation.
  • On line 4, change the error message to suit.
  • On line 10, change 1_1 to the element unique ID.

Require that the value is greater than a number

Note: you can use the Greater Than validator on the Advanced tab of the element settings for this instead of using a custom validator.

1
2
345
6
7
8
9
10
function my_custom_validator($valid, $value, Quform_Element_Field $element)
{
    if ($value <= 18) {        $element->setError('The value is not greater than 18');        $valid = false;
    }
 
    return $valid;
}
add_filter('quform_element_valid_1_1', 'my_custom_validator', 10, 3);
function my_custom_validator($valid, $value, Quform_Element_Field $element)
{
    if ($value <= 18) {
        $element->setError('The value is not greater than 18');
        $valid = false;
    }

    return $valid;
}
add_filter('quform_element_valid_1_1', 'my_custom_validator', 10, 3);

Modify the code to suit the form setup and add your own validation code.

  • On line 3, change the the test to suit your own validation.
  • On line 4, change the error message to suit.
  • On line 10, change 1_1 to the element unique ID.

Matching one token from many

Note: you can use the In Array validator on the Advanced tab of the element settings for this instead of using a custom validator.

1
2
34
5
67
8
9
10
11
12
function my_custom_validator($valid, $value, $element)
{
    $codes = array('12345', '67890', 'abcde', 'fghijk'); 
    if (!in_array($value, $codes)) {
        $element->setError('Invalid code');        $valid = false;
    }
 
    return $valid;
}
add_filter('quform_element_valid_1_1', 'my_custom_validator', 10, 3);
function my_custom_validator($valid, $value, $element)
{
    $codes = array('12345', '67890', 'abcde', 'fghijk');

    if (!in_array($value, $codes)) {
        $element->setError('Invalid code');
        $valid = false;
    }

    return $valid;
}
add_filter('quform_element_valid_1_1', 'my_custom_validator', 10, 3);

Modify the code to suit the form setup and add your own validation code.

  • On line 3, change the the codes to suit your own validation.
  • On line 6, change the error message to suit.
  • On line 12, change 1_1 to the element unique ID.

Multiple custom validators

PHP does not allow you to have more than one function with the same name, so if you have more than one custom validator you will need to choose another function name and also change the second parameter to the add_filter function to your new function name. You can simply add a number to the end of the function name to make it different, or you can use a more descriptive name. Below is an example.

12
3
4
5
6
7
8
9
10
function my_validate_greater_than_18($valid, $value, $element){
    if ($value <= 18) {
        $element->setError('The value is not greater than 18');
        $valid = false;
    }
 
    return $valid;
}
add_filter('quform_element_valid_1_1', 'my_validate_greater_than_18', 10, 3);
function my_validate_greater_than_18($valid, $value, $element)
{
    if ($value <= 18) {
        $element->setError('The value is not greater than 18');
        $valid = false;
    }

    return $valid;
}
add_filter('quform_element_valid_1_1', 'my_validate_greater_than_18', 10, 3);

Notice the highlighted lines, the function name and the second parameter to add_filter have changed to something different from the above examples, allowing more than one custom validator.

Still having trouble? Head over to the forums.

Forums