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

1234
5
6
7
8
add_filter('quform_element_valid_1_3', function ($valid, $value, Quform_Element_Field $element) {    if ($value != 'Foo') {        $element->setError('The value is not Foo');        $valid = false;
    }
 
    return $valid;
}, 10, 3);
add_filter('quform_element_valid_1_3', function ($valid, $value, Quform_Element_Field $element) {
    if ($value != 'Foo') {
        $element->setError('The value is not Foo');
        $valid = false;
    }

    return $valid;
}, 10, 3);

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

  • On line 1, change 1_3 to the element unique ID
  • On line 2, change the the test to suit your own validation
  • On line 3, change the error message to suit
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_3', '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_3', '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_3 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.

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

    return $valid;
}, 10, 3);

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

  • On line 1, change 1_3 to the element unique ID
  • On line 2, change the the test to suit your own validation
  • On line 3, change the error message to suit
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_3', '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_3', '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_3 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.

123
4
56
7
8
9
10
add_filter('quform_element_valid_1_3', function ($valid, $value, $element) {    $codes = array('12345', '67890', 'abcde', 'fghijk'); 
    if (!in_array($value, $codes)) {
        $element->setError('Invalid code');        $valid = false;
    }
 
    return $valid;
}, 10, 3);
add_filter('quform_element_valid_1_3', function ($valid, $value, $element) {
    $codes = array('12345', '67890', 'abcde', 'fghijk');

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

    return $valid;
}, 10, 3);

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

  • On line 1, change 1_3 to the element unique ID
  • On line 2, change the the codes to suit your own validation
  • On line 5, change the error message to suit
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_3', '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_3', '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_3 to the element unique ID

Accessing other form values

Within the validator function you can access the values of other form elements using the code below.

1
2
$form = $element->getForm();
$otherValue = $form->getValue('quform_1_4');
$form = $element->getForm();
$otherValue = $form->getValue('quform_1_4');

Multiple custom validators

This does not apply if you are using the PHP 5.3+ code, simply duplicate the validator code and change the element unique ID to apply the same validator to another element.

If you are using the PHP 5.2 code for creating a custom validator, where the validator function has the name my_custom_validator you will run into a problem if you try and use the same code for a second validator because 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_3', '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_3', '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