Field Validation No Longer Works
-
Contact Form 7 version 4.1
I created a simple case to demonstrate:
Form:
<p>Your Email [email* example-email] <br/> [submit "Send"]</p>
Then I add a validation hook on the “example-email” field. In this example, it should reject all emails except for [email protected]
function example_email_validation($result, $tag) { $example_log_file = '/path/to/your/validate_email.txt'; error_log("Validation called with {$tag['name']}\n", 3, $example_log_file); $fieldName = 'example-email'; // field on the form if($fieldName == $tag['name']){ // this is the field $email = $_POST[$fieldName]; if ($email != '[email protected]') { // would put actual validation here. Hard coding for example. $result['valid'] = false; $result['reason'][$fieldName] = 'Invalid Email'; // error message error_log("Failed Validation: $email \n", 3, $example_log_file); // debug } else { error_log("Passed Validation: $email \n", 3, $example_log_file); // debug } } return $result; } add_filter('wpcf7_validate_email*', 'example_email_validation', 10, 2); add_filter('wpcf7_validate_email', 'example_email_validation', 10, 2);
I do 2 submissions, one with a valid email and one without. The log file reports as expected:
Validation called with example-email Passed Validation: [email protected] Validation called with example-email Failed Validation: [email protected]
But even when the validation fails, the Form UI reports success: “Your message was sent successfully. Thanks.” and I receive an email for both the valid and invalid submissions.
It appears that the return value is ignored. Or has the API changed?
This functionality is important to a number of people using CFDB with CF7 where they use a validation like this to prevent duplicate submissions.
Thank you.
- The topic ‘Field Validation No Longer Works’ is closed to new replies.