Skip Navigation

[Resuelto] Cred maximum upload size for file type field

Este hilo está resuelto. Aquí tiene una descripción del problema y la solución.

Problem: I would like to validate the file size of files uploaded to a custom field in CRED.

Solution: Use the cred_form_ajax_upload_validate hook to test upload file sizes. Use conditionals to check each file individually.

// 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']==889) || ($form_data['id']==846) )
    {
        if(isset($fields['wpcf-resource-file'])) {
          //Retrieve file type
          $file_type_uploaded=$fields['wpcf-resource-file']['field_data']['type'];
           
          //Retrieve file size
          $file_size_uploaded=$fields['wpcf-resource-file']['field_data']['size'];
           
          //check if featured image exists
          if ( ($file_size_uploaded > 1000000) || ($file_type_uploaded != 'application/pdf') )
          {
            //set error message for featured image
            $errors['Resource File'] = 'Sorry the file you have uploaded is not of correct type (PDF) or exceeded 1MB limit';
          } 
        }
    }
    //return result
    return array($fields,$errors);
}

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_ajax_upload_validate

This support ticket is created hace 6 años, 6 meses. 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.

Hoy no hay técnicos de soporte disponibles en el foro Juego de herramientas. Siéntase libre de enviar sus tiques y les daremos trámite tan pronto como estemos disponibles en línea. Gracias por su comprensión.

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)

Este tema contiene 4 respuestas, tiene 2 mensajes.

Última actualización por Tim Elliott hace 6 años, 6 meses.

Asistido por: Christian Cox.

Autor
Mensajes
#902119

Tell us what you are trying to do?
I'm trying to limit the size of files that can be uploaded to a File type field using a CRED form.

Is there any documentation that you are following?
https://toolset.com/forums/topic/types-file-custom-field-and-cred/
I'm trying to use this post as a guide but it isn't working. The post is a bit old so I'm not sure if it should still work or if CRED has changed since.

What is the link to your site?
digitalmarketing.at - but you will need an account to see the site. I'll send you login details if required.

I've changed the code as in the instructions and added to my child theme functions.php:

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 (( 889 == $form_data['id'] ) && (isset($_FILES['wpcf-resource-file']['type'])) && (isset($_FILES['wpcf-resource-file']['size']))) {
  
        //Retrieve file type
        $file_type_uploaded=$_FILES['wpcf-resource-file']['type'];
         
        //Retrieve file size
        $file_size_uploaded=$_FILES['wpcf-resource-file']['size'];
         
        //Validate files uploaded, make sure its PDF file type AND not more than 1MB
        if (!(  ('application/pdf' == $file_type_uploaded) && ($file_size_uploaded < 1048576) )) {
             
            //Validation failed,
            // set error message per field
            //Remove wpcf - prefix here!
            $errors['resource-file']='Sorry the file you have uploaded is not of correct type or exceeded 1MB limit.';   
                 
        } 
  
    }
    //return result
    return array($fields,$errors);
}

889 is the ID of my cred form.
resource-file is the slug of the file upload field.

The file I'm testing the upload with is 3MB (enlace oculto)

Can you see why it doesn't have any effect?
Thanks

#902166

Hi, this code was written before CRED forms implemented AJAX file uploads by default. The cred_form_validate hook is invoked after images are uploaded, so it's not helpful for AJAX upload validation. To validate an upload file size when your form uses AJAX uploads, you can use the cred_form_ajax_upload_validate hook:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_ajax_upload_validate

You can click the orange "+ More" button in the API documentation to see an example.

#902243

Hi Christian

Thanks for your help. Getting there. I've added 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']==889) || ($form_data['id']==846) )
    {
        //Retrieve file type
        $file_type_uploaded=$fields['wpcf-resource-file']['field_data']['type'];
         
        //Retrieve file size
        $file_size_uploaded=$fields['wpcf-resource-file']['field_data']['size'];
         
        //check if featured image exists
        if ( ($file_size_uploaded > 1000000) || ($file_type_uploaded != 'application/pdf') )
        {
            //set error message for featured image
            $errors['Resource File'] = 'Sorry the file you have uploaded is not of correct type (PDF) or exceeded 1MB limit';
        }
    }
    //return result
    return array($fields,$errors);
}

So the aim to have this working on 2 CRED forms 889 and 846.
I'm trying to only allow PDFs under 1MB.
This works for the wpcf-resource-file field, but it also seems to be running the same tests on the featured image field as well, which should allow image files.

I can see why, I think. When I upload a featured image it's triggering the cred_form_ajax_upload_validate hook, but because there is no entry in the wpcf-resource-file field it meets the ($file_type_uploaded != 'application/pdf) if statement so creates the error.

How do I separate this so it only acts on the correct field, or allows different tests for the different fields?

Thanks

#902248

You can use a conditional that tests whether or not the field key is set:

// 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']==889) || ($form_data['id']==846) )
    {
        if(isset($fields['wpcf-resource-file'])) {
          //Retrieve file type
          $file_type_uploaded=$fields['wpcf-resource-file']['field_data']['type'];
          
          //Retrieve file size
          $file_size_uploaded=$fields['wpcf-resource-file']['field_data']['size'];
          
          //check if featured image exists
          if ( ($file_size_uploaded > 1000000) || ($file_type_uploaded != 'application/pdf') )
          {
            //set error message for featured image
            $errors['Resource File'] = 'Sorry the file you have uploaded is not of correct type (PDF) or exceeded 1MB limit';
          } 
        }
    }
    //return result
    return array($fields,$errors);
}

If you want to add more validation code for the featured image, you can add another if / isset statement that tests the _featured_image field.

#902423

Thanks Christian. All working now.