Skip Navigation

[Résolu] Image Validation Hook Not Working on CRED Edit Form

This support ticket is created Il y a 5 années et 4 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.

Aucun de nos assistants n'est disponible aujourd'hui sur le forum Jeu d'outils. Veuillez créer un ticket, et nous nous le traiterons dès notre prochaine connexion. Merci de votre compréhension.

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: Asia/Karachi (GMT+05:00)

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

Dernière mise à jour par julieP Il y a 5 années et 4 mois.

Assisté par: Waqar.

Auteur
Publications
#1291145

I've been using this hook to validate image uploads:-

if ( ( !empty( $fields['wpcf-image-1']['value'] )) && ( isset($_FILES['wpcf-image-1']['type'] )) && ( isset($_FILES['wpcf-image-1']['size'] )) ) {
                
            $file_type_uploaded=$_FILES['wpcf-image-1']['type'];
            $file_size_uploaded=$_FILES['wpcf-image-1']['size'];
            
            if ( ( $file_type_uploaded != 'image/jpeg' ) || ( $file_size_uploaded > 10240 ) ) {
                $errors['image-1']='Please check your image is a JPG/JPEG and smaller than 10KB in size';
            }
        }

but I've noticed the following:-

1. when one or more images are uploaded when the post is first created, the snippet works
2. when the post is subsequently updated using the edit version of the form, the snippet returns the error message for previously uploaded images and any new/replacement ones even if they are of the right type and size.

The setup:-
1. image fields are not repeating fields
2. the media library is not enabled for the frontend
3. I'm using the cred_form_validate hook not AJAX (this is disabled via the cred filter)

Any ideas please?

#1291415

Hi Julie,

Thank you for contacting us and I'd be happy to assist.

The code snippet that you shared won't work for the already uploaded images since it checks for "existence" in the "$_FILES" and not the "emptiness".

When editing a post which already includes an image in the field and no new image is uploaded, the condition in the snippet becomes true even when it shouldn't, due to "isset". In this case, the "$_FILES['wpcf-image-1']['type']" and "$_FILES['wpcf-image-1']['size']" variables exists, but with empty values.

For this reason, it would be best to replace:


if ( ( !empty( $fields['wpcf-image-1']['value'] )) && ( isset($_FILES['wpcf-image-1']['type'] )) && ( isset($_FILES['wpcf-image-1']['size'] )) ) {

With:


if ( ( !empty( $fields['wpcf-image-1']['value'] )) && ( !empty($_FILES['wpcf-image-1']['type'] )) && ( !empty($_FILES['wpcf-image-1']['size'] )) )

I hope this helps.

regards,
Waqar

#1291469

Hi Waqar

Ah yes that subtle but important difference between 'existence' and 'emptiness'; I've changed my snippet and re-tested successfully.

Many thanks for your help; much appreciated