Tell us what you are trying to do?
I am using Toolset Forms to allow users to create a post type. I need to limit the number of files, file size and file type. I am using Toolset's "Image" field type with the setting "Allow multiple instances of this field" and only one in another.
I also want to show the errors in each of the cases.
I am starting from 0 with the process, I tried with the code snippet from the other ticket but it didn't work and I deleted it, exactly what do you need the screenshot of?
Ok - so are you using media library to upload the images or you are using normal file upload button with your form?
If you can share problem URL where you added the form and send me details that what image fields you would like to validate and with that parameters.
*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.
I have set the next reply to private which means only you and I have access to it.
Ok - Here is a sandbox site and you can auto-login to it using the following link:
- hidden link
Can you create a form with your required fields and add that form to a page and send me that page and tell me what fields you want to validate with what.
I mean what files you would like to allow and with what size, share all those validation requirement of yours and I will build the hook for you and share with you.
I have set the next reply to private which means only you and I have access to it.
I've created the form here with all those required fields:
(One single image field, one multiple image field and one file PDF field)
- hidden link
And added the validation hook to "Custom Code" section offered by Toolset:
- hidden link
You should try to use the following validation hook code:
add_action('cred_form_validate', 'func_validate_image_size_and_type_file',10,2);
function func_validate_image_size_and_type_file($error_fields, $form_data) {
$forms = array( 113248 );
// all the valid file types
$file_types = array('image/jpeg','image/png','image/jpg');
$file_field = 'trainer-pdf';
$file_max_size = 2 * 1024 * 1024;
$single_image_field = 'trainer-photo';
$single_image_height = 1200;
$single_image_width = 1200;
$multiple_image_field = 'trainer-photo-gallery';
$multiple_image_width = 1200;
$multiple_image_height = 740;
$multiple_min_instances_required = 2;
// Field data are field values and errors
list($fields,$errors)=$error_fields;
if (in_array($form_data['id'], $forms )) {
if(!empty($_FILES['wpcf-'.$multiple_image_field ]['type'][0])) {
// 1. Minimum number of instances: $multiple_min_instances_required
if ( count( $_FILES['wpcf-'.$multiple_image_field]['name'] ) < $multiple_min_instances_required ) {
$errors['wpcf-'.$multiple_image_field] = "Add at least $multiple_min_instances_required images";
}
//// validate Dimensions
foreach ( $_FILES['wpcf-'.$multiple_image_field]['tmp_name'] as $tmp_image ) {
$sizes = getimagesize( $tmp_image );
if ( $sizes[0] < $multiple_image_width || $sizes[1] < $multiple_image_height ) {
$errors['wpcf-'.$multiple_image_field] = 'every image dimention should be 1200x740.';
}
}
//// validate file types
foreach ( $_FILES['wpcf-'.$multiple_image_field]['type'] as $type ) {
if ( !in_array( $type, $file_types ) ) {
$errors['wpcf-'.$multiple_image_field] = 'multiple Image file type is not supported: ' . $type;
}
}
}
if(!empty($_FILES['wpcf-'.$single_image_field]['name'])) {
//// validate size
$sizes = getimagesize( $_FILES['wpcf-'.$single_image_field]['tmp_name'] );
if ( $sizes[0] < $single_image_height || $sizes[1] < $single_image_width ) {
$errors['wpcf-'.$single_image_field] = 'single image dimention should be 1200x1200.';
}
//// The file types should be JPG, PNG and JPEG.
$image_type = $_FILES['wpcf-'.$single_image_field]['type'];
if ( !in_array( $image_type, $file_types ) ) {
$errors['wpcf-'.$single_image_field] = 'single Image file type is not supported: ' . $image_type;
}
}
if(!empty($_FILES['wpcf-'.$file_field ]['size'])) {
$filesize = $_FILES['wpcf-'.$file_field]['size'];
if ( $filesize >= $file_max_size ) {
$errors['wpcf-'.$file_field] = "file size should less than $file_max_size MB";
}
}
}
////// return errors
return array($fields,$errors);
}
Where:
- You should replace your single image field slug
- You should replace your multiple image field slug
- Adjust the error text
- adjust the required file size and dimensions
I hope the above solution will help you to resolve your issue.