Skip Navigation

[Resolved] Mandatory fields not allowing to submit even condition is false in toolset forms

This thread is resolved. Here is a description of the problem and solution.

This support ticket is created 3 years, 10 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

Tagged: 

This topic contains 24 replies, has 2 voices.

Last updated by pramodk-2 3 years, 9 months ago.

Assisted by: Luo Yang.

Author
Posts
#1906135

I have marked this thread as "waiting for feedback" status

#1907331
Checkbox - Issue three V1.0.png
Checkbox - Issue two V1.0.png
Checkbox - Issue one V1.0.png

Hi Luo,

Earlier as I said, there is a dependency of this code with the issue related to repeated image field.

Meanwhile to test the code provided by you, I have replaced the repeated image field to single image field but the results are not as expected.

To recap the issue to you, at least one checkbox should be selected by the customer in one checkbox field and this should be a validated for 5 fields.

- amenities
- flatmate-preference-age-group
- flatmate-preference-languages
- existing-flatmates-age-group
- existing-flatmates-languages

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'). Please find attached the images for the IDs.

- existing-flatmates-age-group
- existing-flatmates-languages

<?php 
/**
 * Atleast one checkbox is required in post form
*/
toolset_snippet_security_check() or die( 'Direct access is not allowed' );
 
add_filter( 'cred_form_validate', 'tssnippet_at_least_one_checkbox_validation_post_form', 10, 2 );
function tssnippet_at_least_one_checkbox_validation_post_form( $field_data, $form_data ) {
   
    $target_forms = array( 85 );
    $target_field_slug_1 = 'amenities';
    $target_field_slug_2 = 'flatmate-preference-age-group';
    $target_field_slug_3 = 'flatmate-preference-languages';
    $target_field_slug_4 = 'existing-flatmates-age-group';
    $target_field_slug_5 = 'existing-flatmates-languages';
   
    if ( ! in_array( $form_data['id'], $target_forms ) ) {
        return $field_data;
    }
	
	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;
    }
   
    // $field_data contains fields values and errors
    list( $fields, $errors ) = $field_data;
   
    if ( ! isset( $fields[ 'wpcf-' . $target_field_slug_1 ]['value'][0] ) ) {
        $errors[ $target_field_slug_1 ] = 'At least one checkbox is required';
    }
  
    if ( ! isset( $fields[ 'wpcf-' . $target_field_slug_2 ]['value'][0] ) ) {
        $errors[ $target_field_slug_2 ] = 'At least one checkbox is required';
    }
  
    if ( ! isset( $fields[ 'wpcf-' . $target_field_slug_3 ]['value'][0] ) ) {
        $errors[ $target_field_slug_3 ] = 'At least one checkbox is required';
    }
  
    if ( ! isset( $fields[ 'wpcf-' . $target_field_slug_4 ]['value'][0] ) ) {
        $errors[ $target_field_slug_4 ] = 'At least one checkbox is required';
    }
  
    if ( ! isset( $fields[ 'wpcf-' . $target_field_slug_5 ]['value'][0] ) ) {
        $errors[ $target_field_slug_5 ] = 'At least one checkbox is required';
    }
   
    return array( $fields, $errors );
}

There are two issues:

1. The below code does not seem to be working. I am able to submit the form without selecting any checkbox in the checkbox field.

	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;
    }

2. While I removed the code in point 1 (above) and tested, although the image field is added with the image file. It is still saying that the format is in correct. Please find attached the image for the same. This means that the code provided by is affecting the image field.

Image field slug names:

1. property-image-one
2. property-image-two
3. property-image-three
4. property-image-four
5. property-image-five

Please suggest me.

Thanks.

#1909007

Thanks for the details, I can login your website, will update here if find anything

#1909735
Checkbox_latest issue One V1.0.png
Checkbox_latest issue two V1.0.png

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.

#1910363

Thanks for the details:
Issue 1: While selecting 'Occupants - "Yes"' and none of the check boxes in the below fields are selected but still the form is submitted.

You can setup custom codes to do the custom validations:
1) 'Occupants - "Yes"
You can get occupants field value with PHP post variable, like this:

$_POST['occupants'][0]

More help:
hidden link

Check it with PHP codes, like these:

if (  isset( $_POST['occupants'][0] ) &&  $_POST['occupants'][0] == 10 ) ) 
{
...
}

More help:
hidden link

2) none of the check boxes in the below fields are selected but still the form is submitted.
- existing-flatmates-age-group
...

Same as above:
You can get existing-flatmates-age-group field value with PHP post variable, like this:

$_POST['wpcf-existing-flatmates-age-group'][0]

Check it with PHP codes, like these:

isset( $_POST['wpcf-existing-flatmates-age-group'][0] )  ) 

3) Combine them together:

if ( isset( $_POST['occupants'][0] ) &&  $_POST['occupants'][0] == 10 && !isset( $_POST['wpcf-existing-flatmates-age-group'][0]  ) && !isset( $_POST['wpcf-existing-flatmates-languages'][0]  ) ) 
{
$errors[ 'wpcf-existing-flatmates-age-group' ] = 'At least one checkbox is required';
$errors[ 'wpcf-existing-flatmates-languages' ] = 'At least one checkbox is required';
}

It is only an example for your reference, you will need to customize the custom codes according to what you want.

I have edit the code snippet "post_form_checkbox_one" in your website, line 22~26, you can test it in front-end:
hidden link

Issue 2,
According to our support policy,
https://toolset.com/toolset-support-policy/
we prefer one ticket one question, please confirm the issue 1) first, then we can move to issue 2), and I can create another ticket for it.

#1910611

Hi Luo,

Tested with the below steps and not be able to submit the form.

1) Occupants = 'Yes'.
2) Selected at least one checkbox in every check boxes field.
3) Selected image files.

Even after the check boxes in the below fields are selected, it is not allowing to submit the form.

Age Group: At least one checkbox is required : Existing flatmates - One
Languages: At least one checkbox is required : Existing flatmates -One

This issue is because of the code snippet provided by you seems to be incorrect and needs correction.

Could you please have a look at once clearly and let me know what the issue is?

Kindly look at all the possibilities while providing the code snippet please.

Thanks.

#1911447

You were right, it does not work as you mentioned above.

But the custom codes are only an example, as I mentioned above, you need to customize it.

I have modified the codes as below, line 23~30

    if ( isset( $_POST['occupants'][0] ) &&  $_POST['occupants'][0] == 10){ // Occupants = yes
        if( !isset( $_REQUEST['wpcf-existing-flatmates-age-group'][0]) ){
            $errors[ 'wpcf-existing-flatmates-age-group' ] = 'At least one checkbox is required - one';
        }
        if( !isset( $_REQUEST['wpcf-existing-flatmates-languages'][0]) ){
            $errors[ 'wpcf-existing-flatmates-languages' ] = 'At least one checkbox is required - one';
        }
    }

Please test again, check if it is fixed.

#1911697
Checkbox - Issue three V1.0.png

Hi Luo,

Thanks, this is now resolved. Please can you split the ticket for the below issue:

While validating the check boxes fields 'At least one check box to be selected', although the images in the same form are submitted with the correct format (Image file) it is still showing the below issues.

1. You can upload only an image file
2. Correct image files are uploaded

Page : hidden link

Please find attached the image for the same.

Thanks.

New threads created by Luo Yang and linked to this one are listed below:

https://toolset.com/forums/topic/forms-validation-issues/

#1912757

As your request, I have created another thread for the other new questions:
https://toolset.com/forums/topic/forms-validation-issues/

#1912869

My issue is resolved now. Thank you!