Skip Navigation

[Resolved] Validate the image field (Repeating) in the Post Form in Toolset

This thread is resolved. Here is a description of the problem and solution.

Problem:
The user would like to validate an image field(repeatable) in a Toolset form for the image type, size, dimensions, and number of images

Solution:
This will require custom code. Check this example code:

add_action('cred_form_validate', 'my_image_validation_func',10,2);
function my_image_validation_func($error_fields, $form_data) {
    // all the valid file types
    $file_types = array('image/jpeg','image/png','image/jpg');
    $max_size = 5 * 1024 * 1024;
    $forms = array( 1806 );
 
    // 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']))) {
        // 1. Minimum number of photos is '3'.
        if ( count( $_FILES['wpcf-book-images']['name'] ) < 3 ) {
            $errors['wpcf-book-images'] = 'Add at least 3 images';
            return array( $fields, $errors );
        }
 
        // 2. Dimensions of the photo should be at least 350 (height) x 350 (width).
        foreach ( $_FILES['wpcf-book-images']['tmp_name'] as $tmp_image ) {
            $sizes = getimagesize( $tmp_image );
            if ( $sizes[0] < 350 || $sizes[1] < 350 ) {
                $errors['wpcf-book-images'] = 'An image is too small. Images should be at least 350x350';
                return array( $fields, $errors );
            }
        }
 
        // 3. The file types should be JPG, PNG and JPEG.
        foreach ( $_FILES['wpcf-book-images']['type'] as $type ) {
            if ( !in_array( $type, $file_types ) ) {
                $errors['wpcf-book-images'] = 'Image file type is not supported: ' . $type;
                return array( $fields, $errors );
            }
        }
 
        // 4. Maximum file size should be 5MB.
        foreach ( $_FILES['wpcf-book-images']['size'] as $size ) {
            if ( $size >= $max_size ) {
                $errors['wpcf-book-images'] = 'An Image is too big. Images should less than 5MB';
                return array( $fields, $errors );
            }
        }
    }
 
    //return result
    return array($fields,$errors);
}

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate

This support ticket is created 4 years 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
9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 - - 9: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: Africa/Casablanca (GMT+01:00)

This topic contains 19 replies, has 3 voices.

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

Assisted by: Jamal.

Author
Posts
#1902335

Hi Nigel,

Thanks for your reply.

Please do not forget that It is something very important to me and I'm waiting for it.

Thanks.

#1923965

Hi Nigel,

Thought of updating you that I'm waiting for the fix.

Thanks.

#1924081

Hello there,

I checked the internal ticket about this issue and it has not been fixed yet. It has been added to the developer's board but no fix is available yet. We'll let you know when we have something.

Our sincere apologies for the inconvenience.

#2024367

Hello,

This issue has been fixed and released in this week's update, please Please make a full backup and update Toolset plugins, then check if the issue is resolved.

I highly recommend testing the update on a staging site before applying it to the live site.

Let me know what you will get.

#2027169

Hi,

Thanks for updating me.

Thanks.