im trying to make the checkboxes field required but is not working. Im using the below code. Can you advise please
add_filter('cred_form_validate','cred_form_validation', 2, 10);
function cred_form_validation( $field_data, $form_data ) {
// field data are field values and errors
list($fields,$errors)=$field_data;
$forms = array( 211 );
// check to see if this is the correct form
if ( in_array( $form_data['id'], $forms ) ) {
if ( !isset($fields['wpcf-your-company-s-adherence-to']['value'][0]))
$errors['your-company-s-adherence-to'] = 'At least one checkbox is required';
}
add_filter('cred_form_validate','my_validation',10,2);
function my_validation( $field_data, $form_data ) {
// field data are field values and errors
list($fields,$errors)=$field_data;
$forms = array( 211 );
// check to see if this is the correct form
if ( in_array( $form_data['id'], $forms ) ) {
if ( empty($fields['wpcf-your-company-s-adherence-to']['value']))
$errors['wpcf-your-company-s-adherence-to'] = 'At least one checkbox is required';
}
Could you test the validation to see if the section of code get run.
add_filter('cred_form_validate','my_validation',10,2);
function my_validation( $field_data, $form_data ) {
// field data are field values and errors
list($fields,$errors)=$field_data;
$forms = array( 211 );
// check to see if this is the correct form
if ( in_array( $form_data['id'], $forms ) ) {
if ( empty($fields['wpcf-your-company-s-adherence-to']['value']))
die('Empty');
}
return array( $fields, $errors );
}
Could you let me know if after you submit the form you see a Page with only "Empty" written on it ?
Thank you for your answer Shane. Yes I can see the "Empty" word.
My question now is why the message is on the red box and not under the label like in "Company Name" field (check the attachment). Is the way to show the message under the checkboxes field?