Debugging my wpcf7_validate_checkbox function
-
Am attempting to add conditional requirement of agree-to-terms based on a ‘Yes’ value from field New.
Issue is that when agree-to-terms is required it appears in $result with default text regardless of field new’s value like so
[agree-to-conditions] => Array ( [reason] => Please fill the required field. [idref] => )
When agree-to-terms is not required it is never in $result and the custom error message never appears and form is read as valid regardless of checkbox state.
Form fields are currently:
[select new "No" "Yes"] [checkbox* agree-to-conditions "I agree to the Terms and Conditions"]
functions.php code is executing (tested with mail() calls). The values of new and agree-to-terms are seen in callback as expected.
/* CUSTOM VALIDATION FOR CF7 FORM Place a CARE Account */ add_filter( 'wpcf7_validate_checkbox', 'care_account_valid', 10, 2 ); add_filter( 'wpcf7_validate_checkbox*', 'care_account_valid', 10, 2 ); function care_account_valid( $result, $tag ) { $conditions = $_POST['agree-to-conditions']; if ($tag['name'] == 'new' && $_POST['new'] == 'Yes') { if ($conditions[0] != 'I agree to the Terms and Conditions') { $result['valid'] = false; $result['reason']['agree-to-conditions'] = "Please check the box to accept the Terms and Conditions"; } mail('[email protected]', 'test 1.1', $conditions[0] . $_POST['new'] . print_r($result,1)); } else if ($tag['name'] == 'new' && trim($_POST['new']) != 'Yes') { $result['valid'] = true; } return $result; }
Any advice for getting the validation and error message for agree-to-terms dependent on value of new?
Viewing 4 replies - 1 through 4 (of 4 total)
Viewing 4 replies - 1 through 4 (of 4 total)
- The topic ‘Debugging my wpcf7_validate_checkbox function’ is closed to new replies.