Skip Navigation

[Resolved] Validation option missing from checkbox field

This support ticket is created 5 years, 1 month 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 14 replies, has 2 voices.

Last updated by Luo Yang 5 years, 1 month ago.

Assisted by: Luo Yang.

Author
Posts
#1362809
validation-option-missing.jpg

Hi, the validation option is missing from one of custom checkbox fields. Our clients are not filling it out, do you know why it might be missing on this field?

Field name: Building access required
Field Group - hidden link
Form: hidden link

#1363067

Hello,

I assume we are talking about the condition "Location === BioCity Nottingham" in your screenshot:
https://toolset.com/wp-content/uploads/2019/10/1362809-validation_option_missing.jpg

Please try to change it to equal to "=":
Location = BioCity Nottingham

And test again.

The identical to "===" won't work in Toolset form as expected, see our document:
https://toolset.com/documentation/user-guides/types-custom-fields-conditional-display/

#1363493

Hi, thanks I updated that, but I was referring to the 'Required field' option that is missing

#1363949

Thanks for the details, unfortunately, the custom checkboxes field does not support "Required" validation, there isn't such a built-in feature, you can add a feature request for it:
https://toolset.com/home/contact-us/suggest-a-new-feature-for-toolset/

Our developers will evaluate it.

#1364023

Okay I have added the feature request, is there any code I can use in the meantime?

#1364025

If you are using Toolset Forms to setup those checkboxes field values, you can try with cred_form_validate API hook, see workaround of below URL:
https://toolset.com/documentation/adding-custom-code/require-at-least-one-checkbox-of-a-checkboxes-field-to-be-checked-in-a-toolset-form/

#1366511

Hi, thanks for link, I tested the code on our staging site and getting this error:

Parse error: syntax error, unexpected '<', expecting end of file

<?php
/**
* User defined values.
*
* Fill here the IDs of the Forms that you want to affect with this snippet.
* For a single Form, use $target_forms = array( XXX );
* For multiple Forms, use $target_forms = array( XXX, YYY );
* In this example, we target Forms with ID equal to 12345 and 67890.
*
* Define here the Types checkboxes fields that you want to affect with this snippet.
* In this example, we target a Types checkboxes field with a slug of 'hobbies'.
*/
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( 10466, 10894 );
$target_field_slug = 'building-access-required';

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 ]['value'][0] ) ) {
$errors[ $target_field_slug ] = 'At least one checkbox is required';
}

return array( $fields, $errors );
}

#1367025

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

#1367173
#1367301
at_least_one_checkbox_validation.JPG

Thanks for the details, I can log into your website, and have done below modifications in your website:
Dashboard-> Toolset-> Settings-> Custom code

Add a new snippet "at_least_one_checkbox_validation", with the same codes you mentioned above, test it in front-end, it works fine.

I can see the error message: Building access required: At least one checkbox is required

See screenshot at_least_one_checkbox_validation.JPG

#1369023
required.jpg

Hi, I just tested it and can not see the error message for the Building access field. Also added the code to the live site and tested and it's not working on there either. Strange.

#1369567

The filter hook "cred_form_validate" takes effect on server side, so it can be triggered after you fill other required fields and submit the form, then you should see the error message as I mentioned in screenshot:
https://toolset.com/forums/topic/validation-option-missing-from-checkbox-field/#post-1367301

#1369749

Ah okay I understand, I have filled out the form and can now see the error message on the staging site.

But on the live site its still now working, I copied the same code to Dashboard-> Toolset-> Settings-> Custom code

Is there anything else I need to set to get it to work on the Live site?

Thanks

#1369755

You can check these:
1) check the form's ID and field slug of your PHP codes in live site, make sure your using correct form's ID and field slug
2) Make sure you have activated the code snippet, see our document:
https://toolset.com/documentation/adding-custom-code/

#1369947

Great thanks, it's working now.

One strange thing though when you click submit and the page refreshes and show the error message. The date fields that have been filled out, go blank as if they are empty, but still submits the dates when you click submit.

For the user it makes it look like they have to fill the dates out again.

Could you have a look to see if that is normal behavior.

Thanks

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

https://toolset.com/forums/topic/for-the-user-it-makes-it-look-like-they-have-to-fill-the-dates-out-again/