Hi Team,
In my requirement, there are 5 fields with check boxes field type that would require one checkbox in each field as required.
https://toolset.com/documentation/adding-custom-code/require-at-least-one-checkbox-of-a-checkboxes-field-to-be-checked-in-a-toolset-form/
There is a code in the above link but works for only one field at a time.
Please suggest me on how do we achieve one checkbox is required when there are more than one checkboxes field types in one post form.
Thanks,
Pramod.
Hi Pramod,
Welcome to Toolset support and I'd be happy to assist.
To target multiple fields for validation, you can extend that code snippet like this:
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( 12345 );
$target_field_slug_1 = 'field-slug-1';
$target_field_slug_2 = 'field-slug-2';
$target_field_slug_3 = 'field-slug-3';
$target_field_slug_4 = 'field-slug-4';
$target_field_slug_5 = 'field-slug-5';
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 );
}
Note: Please replace "12345" with your actual form's ID and "field-slug-1", "field-slug-2", "field-slug-3", "field-slug-4", and "field-slug-5" with your actual checkboxes type field slugs.
regards,
Waqar
My issue is resolved now. Thank you!
Hi Waqar,
Please let me know in case you need a new ticket to raise this issue.
The below issue is a similar issue and which might need a small tweak to the code.
I have a checkbox field in the user form (edit user) form ID : 144 and slug name: user-languages.
Tried the below code but seems it is not working. Please suggest me.
<?php
/** Atleast one checkbox is required */
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( 144 );
$target_field_slug = 'user-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 ]['value'][0] ) )
{
$errors[ $target_field_slug ] = 'At least one checkbox is required';
}
return array( $fields, $errors );
}
Hi Waqar,
A gentle reminder. Kindly check my reply above and update me.
Thanks.
Thanks for the reminder and I've re-opened this ticket.
I'll need to perform some tests on my website and will update you accordingly.
Thank you for your patience.
Thank you for waiting.
I've tested the code snippet that you've shared on a user edit form on my website and it is working as expected.
Can you please check if the form ID "144" and the field slug "user-languages" is correct?
If the issue still persists, you're welcome to share temporary admin login details along with the link to a page where the form is.
I've set your next reply as private.
Thank you for sharing these details.
During troubleshooting, I noticed that the function name "tssnippet_at_least_one_checkbox_validation" was repeated in two different custom code snippets at WP Admin -> Toolset -> Settings -> Custom Code:
1. post_form_checkbox
2. user_form_checkbox
This is not allowed in PHP and it would explain the critical error when you activate both these snippets.
You can make the function names unique, by including the form ID to make them distinguishable.
For example, the "post_form_checkbox" snippet is for the form with ID "470" so you can rename both instances of the name "tssnippet_at_least_one_checkbox_validation" to "tssnippet_at_least_one_checkbox_validation_470".
Likewise, the "user_form_checkbox" snippet is for the form with ID "144" so you can rename both instances of the name "tssnippet_at_least_one_checkbox_validation" to "tssnippet_at_least_one_checkbox_validation_144".
Hi Waqar,
Thanks for your response.
The function names are changed and re-tested.
There is no critical error but the code seems to be not working which means not validating whether the user selected at least one checkbox.
Code snippet name: user_form_checkbox
Edit Profile page: hidden link
Edit user form: Edit Profile (ID: 144)
User field group name: Profile
User field name: Languages
Slug name: user-languages
Please can you have a look from your end with the user name and password provided to you and let me know what changes are needed.
Thanks.
When I checked the two custom code snippets, I noticed that function names were updated in the definition, but not in the call.
( in my last reply, I suggested to "rename both instances" )
These two screenshots will make this more clear:
hidden link
hidden link
My issue is resolved now. Thank you!
Please close the ticket as it is not allowing me saying the duplicate reply detected.
Thanks for the update and glad that this is resolved now.