Creating a custom validator

This documentation page is for Quform version 1 and may not be applicable for Quform 2 click here to visit the documentation for Quform 2.

Step 1

Add any form element to the form, then find the unique element ID, see Finding the unique element ID.

Step 2

Add the following code to the wp-content/themes/YOUR_THEME/functions.php file (or create a plugin for it) and customise 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->addError('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, $element)
{
    if ($value != 'Foo') {        $element->addError('The value is not Foo');        $valid = false;
    }
 
    return $valid;
}
add_filter('iphorm_element_valid_iphorm_1_1', 'my_custom_validator', 10, 3);
function my_custom_validator($valid, $value, $element)
{
    if ($value != 'Foo') {
        $element->addError('The value is not Foo');
        $valid = false;
    }

    return $valid;
}
add_filter('iphorm_element_valid_iphorm_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 iphorm_1_1 to the unique element ID from Step 1.

Require that the value is greater than a number

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

    return $valid;
}
add_filter('iphorm_element_valid_iphorm_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 iphorm_1_1 to the unique element ID from Step 1.

Matching one token from many

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->addError('Invalid code');        $valid = false;
    }
 
    return $valid;
}
add_filter('iphorm_element_valid_iphorm_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->addError('Invalid code');
        $valid = false;
    }

    return $valid;
}
add_filter('iphorm_element_valid_iphorm_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 iphorm_1_1 to the unique element ID from Step 1.

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 which is also better coding practice. 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->addError('The value is not greater than 18');
        $valid = false;
    }
 
    return $valid;
}
add_filter('iphorm_element_valid_iphorm_1_1', 'my_validate_greater_than_18', 10, 3);
function my_validate_greater_than_18($valid, $value, $element)
{
    if ($value <= 18) {
        $element->addError('The value is not greater than 18');
        $valid = false;
    }

    return $valid;
}
add_filter('iphorm_element_valid_iphorm_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.

Be inspired. © 2024 ThemeCatcher Ltd. 20-22 Wenlock Road, London, England, N1 7GU | Company No. 08120384 | Built with React | Privacy Policy