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
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/
Hi, thanks I updated that, but I was referring to the 'Required field' option that is missing
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.
Okay I have added the feature request, is there any code I can use in the meantime?
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/
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 );
}
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
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
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.
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
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
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/
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