Skip Navigation

[Résolu] Problem setting minimum image size on CRED form

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

Problem: How are image uploads affected by the changes in Toolset Forms 2.4 and what does it mean for validating the image uploads?

Solution: Prior to Forms 2.4 a bespoke ajax uploader was used to pre-upload images (i.e. upload images as soon as selected rather than waiting for the form submission).

Since Forms 2.4 the native WP uploader is used for logged-in users (subject to the settings in the form). It cannot be used for guest users, for whom images are uploaded along with the form submission.

When using the native WP uploader the cred_form_ajax_upload_validate hook can still be used, otherwise the cred_form_validate hook should be used.

Relevant Documentation: https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_ajax_upload_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.

This topic contains 2 réponses, has 2 voix.

Last updated by davidS-53 Il y a 2 années et 1 mois.

Assigned support staff: Nigel.

Auteur
Publications
#1319783

I have a custom image field called profile-picture in a form with Ajax submission enabled. I want to set minimum dimensions for images uploaded to that field. I found some code here: https://toolset.com/forums/topic/featured-image-size-validation-in-form/ which I have adjusted to suit my form IDs and field slug below:

function pkp_set_minimum_image_size( $error_fields, $form_data ) {

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

  if (in_array( $form_data['id'], $forms ) && isset($_FILES['wpcf-profile-picture']['tmp_name']) ) {

    $target_min_width = 600;
    $target_min_height = 900;
    $check = getimagesize( $_FILES['wpcf-profile-picture']['tmp_name'] );
  
    if ( $check !== false ) {

        // check the dimensions only, independent of the orientation.
        // if the height is greater than the width, switch the values for comparison.
        $width = ($check[0] >= $check[1]) ? $check[0] : $check[1];
        $height = ($check[1] < $check[0]) ? $check[1] : $check[0];
  
        if ( $width < $target_min_width || $height < $target_min_height ) {
            $errors['profile-picture'] = "Please upload an image at least 600px wide x 900px high.";
        }
    }

  }

  $field_data = array( $fields, $errors );

  //return result
  return $field_data;

}
add_action( 'cred_form_ajax_upload_validate', 'pkp_set_minimum_image_size', 10, 2 );

However, it doesn't seem to do anything. Any ideas what I might be doing wrong? I'll provide debug info below.

#1320029

Nigel
Supporter

Languages: Anglais (English ) Espagnol (Español )

Timezone: Europe/London (GMT+01:00)

Hi David

Changes were introduced to Forms 2.4 that affect the use of this filter.

Previously, when the user selected an image to include in the form submission the image was uploaded via ajax while the user continued to fill out the form, so that when they finally submitted the form the heavy image files had already been uploaded. This was distinct from whether the form itself was submitted via ajax or not, and the cred_form_ajax_upload_validate filter was for intervening in this image "pre-upload".

Since Forms 2.4 the native WP media uploader can now be used. There is a setting in the form for whether to use it or not. If not then there is no pre-uploading, the image is submitted along with the form itself, and this filter is not triggered.

If you are using the native media uploader, then when users upload an image, this same filter will be triggered, as before, and you should be able to use it in those circumstances.

If not using the native WP media uploader, you can use the cred_form_validate filter to validate the form fields, including image fields.

https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate

#1323741

Thanks Nigel, sorted now.