Require at least one checkbox from a checkboxes group custom field in a CRED form

In a CRED form, you may want to require the User to select at least one checkbox in a checkboxes group custom field. It’s not possible to set up this requirement in wp-admin, but it’s possible to enforce it with the cred_form_validate API hook. Add the following code to your child theme’s functions.php file:

add_filter('cred_form_validate','at_least_one_checkbox_validation', 10, 2);
function at_least_one_checkbox_validation( $field_data, $form_data ) {
    // field data are field values and errors
    list($fields,$errors)=$field_data;
    // forms is a comma-separated list of form IDs where this validation should be applied
    $forms = array( 12345,67890 );
    // validate if the correct CRED form ID
    if ( in_array( $form_data['id'], $forms ) ) {
      if ( !isset($fields['wpcf-cbs']['value'][0]))
        $errors['cbs'] = 'At least one checkbox is required';
    }

    return array( $fields, $errors );
}

Replace 12345,67890 with a comma-separated list of CRED form IDs where you want to apply this validation. Replace ‘cbs’ in both places to match your checkboxes field slug. For example if your checkboxes field slug is “features” then your code should be as follows:

      if ( !isset($fields['wpcf-features']['value'][0]))
        $errors['features'] = 'At least one checkbox is required';

See the documentation for the cred_form_validate API hook for more information.

Validations on WP native fields in a CRED User form

Problem:
Validation and Required field option should work at once in CRED User form for native WP Fields (e.g. Last Name), same as builtin CRED validation style.

Solution:
1. Add this code in your theme’s or child theme’s functions.php file:

add_filter('cred_filter_field_before_add_to_form', 'required_fields_func', 10, 2);
function required_fields_func($field, $computed_values){
    if(in_array($field['id'], array('last_name', 'adresse'))){
        $field['data']['validate']['required'] = array ( 
            'active' => 1, 
            'value' => 1, 
            'message' => 'This field is required.'
        ) ;
    }
    return $field;
}

— Make sure to replace ‘last_name’, ‘adresse’ with your field slugs on line #3
— You can modify the message text as needed in above code on line #7.

2. OR If you want to have JS validation, you can use this (modification will be needed):
https://wp-types.com/forums/topic/cred-user-field-not-required/#post-567100

Related Ticket:
https://wp-types.com/forums/topic/cred-user-field-not-required/

Validate width and height of image with CRED

You can retrieve that data through tmp_name. The first and second indexes of this array are width and height, respectively

function validate_image_width_height($field_data, $form_data){
  
list($fields,$errors)=$field_data;  

$image_info = $fields['wpcf-image-1']['value']['tmp_name'];
$image_width = $image_info[0];
$image_width = $image_info[1];

if ($image_width < 100){
     $errors['image-1'] = __('Wrong width', 'nbt');
}

var_dump($fields['wpcf-image-1']);
 
return array($fields,$errors);
}

add_filter('cred_form_validate','validate_image_width_height',10,2);

Use the Code Snippet button to enter php and html snippets.