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, 11 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, 10 months ago.

Assisted by: Luo Yang.

Author
Posts
#1889741

Hi Team,

In my requirement, a field group which contains mandatory fields are within a conditional group in a post form.

When the condition is true, the field group will open and should fill the details of the fields since they are mandatory.

When the condition is false, the field group will disappear and will not fill even though they are set as mandatory which is expected as per my understanding.

However, when the condition is false, the field group is disappeared but not allowing to submit the form since the fields in it are set as mandatory.

Please suggest me.

hidden link

Thanks.

#1890355

Hello,

You are right, you should be able to submit the form in this case.
I have tried it in my localhost with a fresh WP installation + the latest version of Toolset plugins, it works fine, and I can submit the form when the condition field is false(I am using a custom checkbox field as the condition field).

Please check these:
1) Make sure you are using the latest version of Toolset plugins, you can download them here:
https://toolset.com/account/downloads/

2) In case it is a compatibility problem, please deactivate all other plugins, and switch to wordpress default theme 2021, deactivate all custom PHP/JS code snippets, and test again

3) Also check if there is any PHP/JS error in your website:
https://toolset.com/documentation/programmer-reference/debugging-sites-built-with-toolset/

#1890455

Hi Luo,

Many thanks for your reply.

Performed testing using the suggestions below but not working.

1. Latest version of the Toolset plugins are in use.
I have purchased toolset plugins very recently after that there are no new updates to the toolset plugins.

2. Deactivated all the PHP/JS code snippets and checked.
Also, since my website is not advertised for public to use therefore created a new form with only these conditions where PHP/JS code snippets are not used and checked but still the issue remains the same.

Please look at the link below where you can check the issue I am facing.
hidden link

3. Referred to the below link and performed JS error check but seems no issue, but not be able to check for PHP error where I am struggling to create a error file. But I do not think this is something an issue with PHP error but not sure.
https://toolset.com/documentation/programmer-reference/debugging-sites-built-with-toolset/

Tried using the same in toolset test site with Astra theme and it is working fine. Replicated the same in my website but not sure what is the issue.

Let me know in case you would need my backend access for you to have a look if it can help.

Thanks.

#1890481

Thanks for the details, I have tried the URL you mentioned above, I can submit the form, but it will return error message:
Gender This field is required.
Occupation This field is required.

These error message from server side, it seems you are using empty value as default option value

Those two custom fields:
- Gender
- Occupation

I suggest you try these:
Dashboard-> Toolset-> Custom Fields, fine and edit those two fields, setup the default option's value to something else, for example: 0, then test again

#1890663
Conditional group2.png

Hi Luo Yang,

Many thanks once again for your quick reply.

There was one more conditional group inside a conditional group, I have removed it as it is no longer required.
Once it is removed it is not asking for select fields inside the conditional group to select one and it is working fine.
Please find attached the image where I have removed the nested conditional group.

However, I have provided the below code snippet for at least one checkbox to select which is in a conditional group.

Once the conditional group is false the field group in the post form is disappeared but not allowing to submit asking for checkbox to select.

<?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', 10, 2 );
function tssnippet_at_least_one_checkbox_validation( $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;
    }
  
    // $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 );
}

Thanks.

#1890709

Hi Luo Yang,

Replicated the issue in the below link for you to check.
hidden link

Post form used for that is below:

[credform]
	[cred_field field='form_messages' class='alert alert-warning']
	<h4>DETAILS</h4>
	<h4>PROPERTY INFORMATION</h4>
	<div class="form-group">
		<label>[cred_i18n name='occupants-label']<strong>Occupants</strong>[/cred_i18n]</label> <span class="required">*</span>
		[cred_field field='occupants' force_type='taxonomy' output='bootstrap' display='select' single_select='true']
	</div>
	[cred_show_group if="(( $(occupants) eq '10' ))" mode="fade-slide"]
	<h4>EXISTING FLATMATES</h4>
	<div class="row">
	<div class="col-md-6">
	<div class="form-group">
		<label>[cred_i18n name='existing-flatmates-gender-label']<strong>Gender</strong>[/cred_i18n]</label> <span class="required">*</span>
		[cred_field field='existing-flatmates-gender' force_type='field' class='form-control' output='bootstrap']
	</div>
	</div>
	<div class="col-md-6">
	<div class="form-group">
		<label>[cred_i18n name='existing-flatmates-occupation-label']<strong>Occupation</strong>[/cred_i18n]</label> <span class="required">*</span>
		[cred_field field='existing-flatmates-occupation' force_type='field' class='form-control' output='bootstrap']
	</div>
	</div>
	</div>
	<div class="form-group">
		<label>[cred_i18n name='existing-flatmates-age-group-label']<strong>Age Group</strong>[/cred_i18n]</label> <span class="required">*</span>
		[cred_field field='existing-flatmates-age-group' force_type='field' class='form-control' output='bootstrap']
	</div>
	<div class="form-group">
		<label>[cred_i18n name='existing-flatmates-languages-label']<strong>Languages</strong>[/cred_i18n]</label> <span class="required">*</span>
		[cred_field field='existing-flatmates-languages' force_type='field' class='form-control' output='bootstrap']
	</div>
	[/cred_show_group]
	
	[cred_field field='form_submit' output='bootstrap' value='Submit' class='btn btn-primary btn-lg']
[/credform]

I would need to resolve this issue and cannot be ignored, please suggest me.

Thanks.

#1891161

It is a custom codes problem, please provide a test site with the same problem, also point out the URL where I can edit your custom PHP codes, I need to test and debug it in a live website, thanks

#1891387

Thanks for the details, I have done below modifications in your website:
Edit the code snippet "post_form_checkbox_required", add below lines 20~22:

  	if ( ( isset( $_POST['wpcf-i-am'] ) && ($_POST['wpcf-i-am'] != 1) ) || ( isset( $_POST['occupants'][0] ) && ($_POST['occupants'][0] != 10) ) ) {
        return $field_data;
    }

Please test again, check if it is what you want.

#1891439

Hi Luo Yang,

There are five field slugs in the custom code:

    $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';

The condition which you have added now should be valid only for the below and others should work as it is without change.

    $target_field_slug_4 = 'existing-flatmates-age-group';
    $target_field_slug_5 = 'existing-flatmates-languages';

In order to replicate it, the form which is created for you does not have these and you may have not considered. Please find the below page for the actual form.

Form name:List A Room (ID: 85)
hidden link

Kindly customise the code accordingly and suggest me.

Thanks.

#1893643

You can use PHP function unset() to remove those post variables, for example:

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

More help:
hidden link

#1893723
Repeated image field - Issue one V1.0.png

Hi Luo Yang,

While testing, I have filled the form without at least one check box selected, it is showing an error (At least one checkbox is required) which is expected but along with that the image (repeated) field is auto incremented.

Please find attached the image where the field shown with the arrow are not selected but are auto incremented.

This is not only with the code snippet provided by you, it is also while checking the code snippet provided in the below ticket.

https://toolset.com/forums/topic/validate-the-image-field-repeating-in-the-post-form-in-toolset/#post-1892957

Not sure whether there are any changes required in the codes or image (repeated) field required changes.

I have highlighted this issue in the above ticket and waiting for the response.

I will test your code once after getting the response for the above ticket and let you know.

Until then, I do not close the ticket and update you accordingly.

Thanks.

#1894457

Please update here when you still need assistance for it, thanks

#1898241

Hi Luo,

I'm still waiting for the update from other support ticket.

Thanks.

#1898263

I have marked this thread as "waiting for feedback" status, you can update here when you need more assistance.

#1906021

Hi Luo,

Thanks. I'm writing this comment now to ensure this ticket is open.

Thanks.