Hi Luo,
Created two code snippets and performed testing.
Recreated the issue in hidden link with form ID "Test form (ID: 1137)".
1. Code snippet name : post_form_checkbox_one
This code snippet is created to validate whether at least one check box in the below fields are selected.
- amenities
- flatmate-preference-age-group
- flatmate-preference-languages
2. Code snippet name : post_form_checkbox_two
This code snippet is created to validate whether at least one check box in the below fields are selected. Also, the condition to check at least one checkbox for the below fields is required only when Occupants is 'Yes' (Taxonomy ID '10' - created as a select field in form) OR I am is 'A flatmate' (ID '1').
- existing-flatmates-age-group
- existing-flatmates-languages
Please refer to the codes provided in the above code snippet names.
We are almost there but with the two issues found...
Issue 1: While selecting 'Occupants - "Yes"' and none of the check boxes in the below fields are selected but still the form is submitted.
- existing-flatmates-age-group
- existing-flatmates-languages
I think, the below code needs a tweak to allow the validation when Occupants is set to 'Yes'.
Note : Occupants is a taxonomy with ID 10 for 'Yes'. Please find attached the image for the same.
if ( ( isset( $_POST['wpcf-i-am'] ) && ($_POST['wpcf-i-am'] != 1) ) || ( isset( $_POST['occupants'][0] ) && ($_POST['occupants'][0] != 10) ) )
{
unset($_POST['wpcf-existing-flatmates-age-group'], $_POST['wpcf-existing-flatmates-languages']);
return $field_data;
}
Issue 2: While submitting the form with below steps, even after submitting the correct image file, the image field is notifying 'You can upload only an image file' but good thing is it is allowing to submit.
1. Without at least one check box in any field is selected.
2. Correct image files are uploaded.
While submitting the form with below steps, even after submitting the correct image file, the image field is notifying 'You can upload only an image file' and 'Field is required' but good thing is it is allowing to submit. Please find attached the image.
1. Without at least one check box in any field is selected.
2. Correct image files are uploaded.
3. Tried to submit the form twice with above conditions.
Thanks.