Sauter la navigation

[Résolu] How to validate image size for multiple image fields using frontend forms

Ce fil est résolu. Voici une description du problème et la solution proposée.

Problem:
How to validate image size for multiple image fields using frontend forms

Solution:
To validate the form field in general you can use the Toolset form's hook "cred_form_validate".

You can find the proposed solution in this case with the following reply:
=> https://toolset.com/forums/topic/trying-to-validate-non-repeating-image-fields-on-front-end-user-form/#post-2334355

Relevant Documentation:
=> https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/
=> https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate

This support ticket is created Il y a 2 années et 8 mois. 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)

Ce sujet contient 4 réponses, a 2 voix.

Dernière mise à jour par MargeP6083 Il y a 2 années et 8 mois.

Assisté par: Minesh.

Auteur
Publications
#2333981

I'm trying to validate image fields that each are coming from a different custom post field. The image fields are
_featured_image, talent-image-1, talent-image-2, talent-image-3, talent-image-4.

I would like to do something like this ticket, but am not sure how to go about creating an array for the individual image fields. https://toolset.com/forums/topic/validate-the-image-field-repeating-in-the-post-form-in-toolset

add_action('cred_form_validate', 'talent_image_validation_func',10,2);

function talent_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( 66, 185 );

// Field data are field values and errors
list($fields,$errors)=$error_fields;

if (in_array($form_data['id'], $forms ) && (isset($_FILES['wpcf-images']['type']))) {

// 1. The file types should be JPG, PNG and JPEG.
foreach ( $_FILES['wpcf-images']['type'] as $type ) {
if ( !in_array( $type, $file_types ) ) {
$errors['wpcf-images'] = 'Image file type is not supported: ' . $type;
return array( $fields, $errors );
}
}

// 2. Maximum file size should be 5MB.
foreach ( $_FILES['wpcf-images']['size'] as $size ) {
if ( $size >= $max_size ) {
$errors['wpcf-images'] = 'An Image is too big. Images should less than 5MB';
return array( $fields, $errors );
}
}
}

//return result
return array($fields,$errors);
}

Thank you!

#2334307

Minesh
Supporter

Les langues: Anglais (English )

Fuseau horaire: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Can you please try to use the following code to validate your image fields and try to resolve your issue.

You can add the following code to "custom code" section offered by Toolset:
=> https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/

add_action('cred_form_validate', 'talent_image_validation_func',10,2);
function talent_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( 66, 185);

// Field data are field values and errors
list($fields,$errors)=$error_fields;

if (in_array($form_data['id'], $forms ) ) {

$form_fields = array("_featured_image","wpcf-talent-image-1","wpcf-talent-image-2", "wpcf-talent-image-3", "wpcf-talent-image-4");

// 1. The file types should be JPG, PNG and JPEG.
  foreach($form_fields as $k=>$v):
		 $type = $_FILES[$v]['type'];
			if ( !in_array( $type, $file_types ) ) {
				$errors[$v] = 'Image file type is not supported: ' . $type;
  			}
   endforeach;
	
 
// 2. Maximum file size should be 5MB.
   foreach($form_fields as $k=>$v):
              	$size = $_FILES[$v]['size'];
				if ( $size >= $max_size ) {
					$errors[$v] = 'An Image is too big. Images should less than 5MB';
                 
				
				}
   endforeach;
  
}

//return result
return array($fields,$errors);
}
#2334343
image error messages.jpg

Hi, thanks for this!
It works to validate the fields, but the error message shows in a list fields that are not in error. How can I have only the field in error listed?

Thanks again!

Marge

#2334355

Minesh
Supporter

Les langues: Anglais (English )

Fuseau horaire: Asia/Kolkata (GMT+05:30)

Can you please try to use the following code:

add_action('cred_form_validate', 'talent_image_validation_func',10,2);
function talent_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( 66, 185);
 
// Field data are field values and errors
list($fields,$errors)=$error_fields;
 
if (in_array($form_data['id'], $forms ) ) {
 
$form_fields = array("_featured_image","wpcf-talent-image-1","wpcf-talent-image-2", "wpcf-talent-image-3", "wpcf-talent-image-4");
 
// 1. The file types should be JPG, PNG and JPEG.
  foreach($form_fields as $k=>$v):
         $type = $_FILES[$v]['type'];
            if (!empty($type) and  !in_array( $type, $file_types ) ) {
                $errors[$v] = 'Image file type is not supported: ' . $type;
            }
   endforeach;
     
  
// 2. Maximum file size should be 5MB.
   foreach($form_fields as $k=>$v):
                $size = $_FILES[$v]['size'];
                if (!empty($size) and  $size >= $max_size ) {
                    $errors[$v] = 'An Image is too big. Images should less than 5MB';
                  
                 
                }
   endforeach;
   
}
 
//return result
return array($fields,$errors);
}

I've just added the additional check for type and size:

  if (!empty($type) and  !in_array( $type, $file_types ) ) {
 if (!empty($size) and  $size >= $max_size ) {
#2334471

This works great! Many thanks!

My issue is resolved now. Thank you!