Skip Navigation

[Resolved] Minimum number of images in Toolset forms

This support ticket is created 3 years, 9 months 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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

This topic contains 7 replies, has 2 voices.

Last updated by pramodk-2 3 years, 9 months ago.

Assisted by: Minesh.

Author
Posts
#1940141

Hi Team,

Please suggest me the code to validate to ensure customers are uploading minimum no. of images is '3'.

This is a image (repeated) field.

Not using Ajax while submitting the form.

The images are added from WordPress media library.

Thanks.

#1940329

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

As I understand - you have a Toolset post form having repeating image field and you want to make sure that people must add at least three images and the image is selected using the media library - correct?

Please correct me if that is not the case you are having.

#1940363

Yeah,

You are right.

Thanks

#1940403

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

You should try to add the following Toolset form validation hook "cred_form_validate" code to "Custom Code" section offered by Toolset:
- https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/

add_action('cred_form_validate', 'func_validate_image_count',10,2);
function func_validate_image_count($error_fields, $form_data) {
    $forms = array( 99999 );
 
    // Field data are field values and errors
    list($fields,$errors)=$error_fields;
 
    if (in_array($form_data['id'], $forms ) && (isset($_FILES['wpcf-book-images']['type']))) {
      
        if ( count( $_FILES['wpcf-book-images']['name'] ) < 3 ) {
            $errors['wpcf-book-images'] = 'Add at least 3 images';
            return array( $fields, $errors );
        }
 
 
    //return result
    return array($fields,$errors);
}

Where:
- Replace 99999 with your original form ID
- Replace book-images with your original field slug

More info:
- https://toolset.com/documentation/programmer-reference/cred-api/#cred_before_save_data

#1944149

Hi Minesh,

I will check and update on this by this sunday.

Thanks.

#1947023

Hi Minesh,

I have tested this, it is not working.

As mentioned, the images are to be taken from the media library.

This seems to be working while taking from the system, but no changes while the images are taken from the wordpress media library.

Is the code valid for both while images taken from the wordpress media library and while taken from the system (not through media library)?

Thanks.

#1949413

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I would like to know, do you mean that you want to validate the maxinum number of image selection at the time user select the images from media uploader dialog box?

As, the validation hook I shared will be run when you submit the form.

#1954471

My issue is resolved now. Thank you!