Skip Navigation

[Resolved] Limit Size of uploaded image not working!

This thread is resolved. Here is a description of the problem and solution.

Problem:

Validate file size of multiple instances image field.

Solution:

It needs to use filter hook cred_form_ajax_upload_validate, for example:

https://toolset.com/forums/topic/limit-size-of-uploaded-image-not-working/#post-1088293

Relevant Documentation:

This support ticket is created 6 years, 3 months 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
- 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/Hong_Kong (GMT+08:00)

This topic contains 4 replies, has 2 voices.

Last updated by Luo Yang 6 years, 3 months ago.

Assisted by: Luo Yang.

Author
Posts
#1087755

I am following the code snippet here: https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_ajax_upload_validate

I have added the code but this showing always Error even if the image is less than 1 megabytes.
Is this happening because its repeated image field? if yes how should be done?

add_filter('cred_form_ajax_upload_validate','my_validation',10,2);
function my_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']==103)
    {
        //check if featured image exists o
        if ($fields['_featured_image']['field_data']['size'] > 1000000)
        {
            //set error message for featured image
            $errors['_featured_image'] = 'Wrong size image';
        }
    }
    //return result
    return array($fields,$errors);
}
#1087759

Tried to follow this ticket too but no success: https://toolset.com/forums/topic/limit-upload-file-size-for-repeating-image-custom-field/#post-288812

//Limit upload file size for repeating image custom field
add_filter('cred_form_validate','story_image_size_validation',10,2);
function story_image_size_validation($field_data, $form_data)
{
    list($fields,$errors)=$field_data;
    $arr = array(103, 102);
    if (in_array($form_data[id], $arr)){
        if(isset($fields['wpcf-car-images']['file_data']['size'])){
            if($fields['wpcf-car-images']['file_data']['size']>1000){
                $errors['car-images']='the image size should not be more than 1000 bytes';
            }
        }
    }
    return array($fields,$errors);
}
#1088293

Hello,

In your case, it needs to use filter hook cred_form_ajax_upload_validate, for example:
1) I assume we are talking about a custom multiple-instances image field "car-images", which is created with Types plugin
2) You can modify your PHP codes as below:

add_filter('cred_form_ajax_upload_validate','my_validation',10,2);
function my_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']==123456)
    {
        //get the latest image size
		$size = end($fields['wpcf-car-images']['field_data']['size']);
        if ($size > 1000000)
        {
            $errors['wpcf-car-images'] = 'the image size should not be more than 1M bytes';
        }
    }
    return array($fields,$errors);
}

Please replace 123456 with your Toolset form's ID, and test again

#1092397

Yes Wokrd. Thank you Luo!!

Here is final Code

add_filter('cred_form_ajax_upload_validate','my_validation',10,2);
function my_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']==103)
    {
        //get the latest image size
        $size = end($fields['wpcf-car-images']['field_data']['size']);
        if ($size > 5000000)
        {
            $errors['wpcf-car-images'] = 'the image size should not be more than 5M bytes';
        }
    }
    return array($fields,$errors);
}
#1092514

You are welcome