Skip Navigation

[Resolved] set size, weight and file type limits in toolset forms image field

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 6 replies, has 2 voices.

Last updated by Minesh 5 months, 1 week ago.

Assisted by: Minesh.

Author
Posts
#2702249
Captura de pantalla 2024-06-12 085157.png

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.

Is there any documentation that you are following?
https://toolset.com/forums/topic/restricting-file-size-and-types-on-file-upload-cred-form/#post-1411409

What is the link to your site?
hidden link

#2702337

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Frist of all - Can you please send me edit page full page screenshot of your form where you added the form field from backend admin?

#2702381

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?

#2702840

Minesh
Supporter

Languages: English (English )

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

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.

#2703211

Minesh
Supporter

Languages: English (English )

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

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.

#2703535
Captura de pantalla 2024-06-20 100943.png
Captura de pantalla 2024-06-20 100937.png
Captura de pantalla 2024-06-20 100931.png

hidden link
ther is the form

hidden link
here we have the original form

i only want to validate size and weight of "foto de portada" and "foto de producto fields" and "ficha tecnica" only weight

#2703650

Minesh
Supporter

Languages: English (English )

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

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.