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 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.
No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.
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 3 years, 7 months ago.
Assisted by: Jamal.