One useful code snippet for custom validation in WP Forms is to add a custom validation rule to check if a specific field value matches a predefined value. This can be useful in scenarios where you want to ensure that a user enters a specific value in a form field.
Here’s an example code snippet that demonstrates how to add a custom validation rule to a WP Forms field:
/**
* Custom validation rule to check if a field value matches a predefined value.
*
* @param array $fields
* @param array $entry
* @param array $form_data
*
* @return array
*/
function wpsnippets_custom_validation_rule( $fields, $entry, $form_data ) {
// Get the field value
$field_value = $entry[ $fields['id'] ];
// Define the predefined value
$predefined_value = 'example';
// Check if the field value matches the predefined value
if ( $field_value !== $predefined_value ) {
// Add an error message to the field
$fields['failed_validation'] = true;
$fields['errorMessage'] = 'Please enter the correct value.';
}
return $fields;
}
add_filter( 'wpforms_process_validate_field', 'wpsnippets_custom_validation_rule', 10, 3 );
In this code snippet, we define a custom validation rule using the wpforms_process_validate_field
filter. The function wpsnippets_custom_validation_rule
accepts three parameters: $fields
, $entry
, and $form_data
. We retrieve the field value from the $entry
array using the field ID, and compare it with a predefined value. If the values don’t match, we set the failed_validation
flag to true and add an error message to the field.
You can modify the $predefined_value
variable to match your specific requirement. This code snippet can be useful when you want to enforce a specific value in a form field, such as a confirmation field for password or email address.
Examples
Example 1: Custom validation for a WP Forms field
This example demonstrates how to add custom validation to a specific field in a WP Forms form. In this case, we will add a custom validation rule to check if the entered value is a valid email address.
function wpsnippets_custom_email_validation( $errors, $fields ) {
if ( ! is_email( $fields['email'] ) ) {
$errors['email']['invalid'] = 'Please enter a valid email address.';
}
return $errors;
}
add_filter( 'wpforms_process_validate_field', 'wpsnippets_custom_email_validation', 10, 2 );
Explanation:
- The
wpforms_process_validate_field
filter hook is used to add custom validation to a specific field in a WP Forms form. - The
wpsnippets_custom_email_validation
function is hooked into this filter and receives two parameters:$errors
(an array of validation errors) and$fields
(an array of form field values). - Inside the function, we check if the entered email address is valid using the
is_email()
function. - If the email address is not valid, we add a custom error message to the
$errors
array for the ’email’ field. - Finally, we return the modified
$errors
array.
Example 2: Custom validation for multiple fields
This example demonstrates how to add custom validation to multiple fields in a WP Forms form. In this case, we will add a custom validation rule to check if both the ‘name’ and ‘message’ fields are not empty.
function wpsnippets_custom_fields_validation( $errors, $fields ) {
if ( empty( $fields['name'] ) ) {
$errors['name']['empty'] = 'Please enter your name.';
}
if ( empty( $fields['message'] ) ) {
$errors['message']['empty'] = 'Please enter a message.';
}
return $errors;
}
add_filter( 'wpforms_process_validate_field', 'wpsnippets_custom_fields_validation', 10, 2 );
Explanation:
- Similar to the previous example, we use the
wpforms_process_validate_field
filter hook to add custom validation to multiple fields in a WP Forms form. - The
wpsnippets_custom_fields_validation
function is hooked into this filter and receives the$errors
and$fields
parameters. - Inside the function, we check if the ‘name’ field is empty and add a custom error message to the
$errors
array if it is. - Similarly, we check if the ‘message’ field is empty and add a custom error message if it is.
- Finally, we return the modified
$errors
array.
Example 3: Custom validation with conditional logic
This example demonstrates how to add custom validation with conditional logic to a WP Forms form. In this case, we will add a custom validation rule to check if a specific field is required based on the value of another field.
function wpsnippets_custom_conditional_validation( $errors, $fields ) {
if ( $fields['country'] === 'USA' && empty( $fields['zipcode'] ) ) {
$errors['zipcode']['empty'] = 'Please enter a ZIP code.';
}
return $errors;
}
add_filter( 'wpforms_process_validate_field', 'wpsnippets_custom_conditional_validation', 10, 2 );
Explanation:
- Again, we use the
wpforms_process_validate_field
filter hook to add custom validation to a WP Forms form. - The
wpsnippets_custom_conditional_validation
function is hooked into this filter and receives the$errors
and$fields
parameters. - Inside the function, we check if the value of the ‘country’ field is ‘USA’ and if the ‘zipcode’ field is empty.
- If both conditions are met, we add a custom error message to the
$errors
array for the ‘zipcode’ field. - Finally, we return the modified
$errors
array.