Skip Navigation

[Resolved] Restricting File Size and Types on File Upload CRED form

This support ticket is created 5 years ago. 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 3 replies, has 2 voices.

Last updated by Christian Cox 5 years ago.

Assisted by: Christian Cox.

Author
Posts
#1406687
1. error.PNG

I am trying to: Restricting File Size and Types to PDF on File Upload CRED form

Link to a page where the issue can be seen, but in order to see this issue, you will have to be logged in, as this page is restricted to the public. Ill provide you with test user login.
hidden link

If you upload a file that doesn't conform to the condition through your media library, then i see the error in the media library. See image 1. error.
if i chose an image from the media library that is already uploaded, no error displays and image is allowed.
If i set the form to not use the media library, but to upload form pc, then no error message is displayed, and image upload is allowed.

If used this code:

// For the resource file upload - check that the file is a PDF under 1MB in size.
add_filter('cred_form_ajax_upload_validate','cred_filetype_size_validation',10,2);
function cred_filetype_size_validation($error_fields, $form_data)
{
    //field data are field values and errors
    list($fields,$errors)=$error_fields;
    //validate if specific form
    if ( ($form_data['id']==23782) || ($form_data['id']==12407) )
    {
        //Retrieve file type
        $file_type_uploaded=$fields['wpcf-pers-dev-success-story']['field_data']['type'];
          
        //Retrieve file size
        $file_size_uploaded=$fields['wpcf-pers-dev-success-story']['field_data']['size'];
          
        if ( ($file_size_uploaded > 1000000) || ($file_type_uploaded != 'application/pdf') )
        {
            $errors['pers-dev-success-story'] = 'Sorry the file you have uploaded is not of correct type (PDF) or exceeded 1MB limit';
        }
    }
    //return result
    return array($fields,$errors);
}
#1410679

If you upload a file that doesn't conform to the condition through your media library, then i see the error in the media library. See image 1. error.
Okay, this seems like the correct behavior unless I'm misunderstanding something.

if i chose an image from the media library that is already uploaded, no error displays and image is allowed.
Right, the AJAX upload hook is only triggered when you are uploading something, not when you select something that already exists in the library. To validate that item, you would need the cred_form_validate hook.

If i set the form to not use the media library, but to upload form pc, then no error message is displayed, and image upload is allowed.
Yes, the AJAX upload hook only applies to AJAX uploads, and if you disable the Media Library then files are not uploaded via AJAX. You could use the cred_form_validate hook in this case as well.

We have documentation for this hook available here:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate

Let me know if you have additional questions about this hook and I can provide some more guidance.

#1411409

Ive disabled the media library and changed my code to this:

add_filter('cred_form_validate','cred_filetype_size_validation',10,2);
function cred_filetype_size_validation($field_data, $form_data)
{
    // Field data are field values and errors 
    list($fields,$errors)=$field_data;
  
    //Run the rest of code for this CRED ONLY and IF the file is upload type and size are set.
    if (( 23782 == $form_data['id']) && (isset($_FILES['wpcf-pers-dev-success-story']['type'])) && (isset($_FILES['wpcf-pers-dev-success-story']['size']))) {
  
        //Retrieve file type
        $file_type_uploaded=$_FILES['wpcf-pers-dev-success-story']['type'];
         
        //Retrieve file size
        $file_size_uploaded=$_FILES['wpcf-pers-dev-success-story']['size'];
         
        //Validate files uploaded, make sure its PDF file type AND not more than 100kB
        if (!(  ('application/pdf' == $file_type_uploaded) && ($file_size_uploaded < 100000) )) {
             
            $errors['pers-dev-success-story']='Sorry the file you have uploaded is not of the correct type or exceeded 100kB limit.';   
                 
        } 
  
    }
    //return result
    return array($fields,$errors);
}

The form is now not submitting, but I'm not getting an error message at all.

#1412357
Screen Shot 2019-12-16 at 4.44.06 PM.png

It looks like the form message field was deleted:

[cred_field field='form_messages' class='alert alert-warning']

Without it, you won't see error messages. So I added it back and tested with a big JPG image. I see an error now, can you check? Also note the preferred way to access file information is in the $fields array. Click "+ More" to see code examples here, including the array syntax for repeating fields:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate