Skip Navigation

[Resolved] File upload validation

This support ticket is created 2 years, 9 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 2 replies, has 2 voices.

Last updated by TomW5440 2 years, 9 months ago.

Assisted by: Luo Yang.

Author
Posts
#2350405

Tell us what you are trying to do?

I was using repeatable file upload field and had working validation for max file upload size and pdf file type. However I since changed the repeatable field to 4 non repeatable file upload fields and now the validation doesn't work.

I was planning on repeating this code for each file upload field and modifying the file field, filter name and function name but I can't get it validate the first file upload field.

//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(135);
    if (in_array($form_data[id], $arr)){
        if(isset($fields['wpcf-wills-and-expression-of-wish-forms']['file_data']['size']['type'])){
            if($fields['wpcf-wills-and-expression-of-wish-forms']['file_data']['size']>10000){
                $errors['wills-and-expression-of-wish-forms']='the image size should not be more than 10000 bytes';
            }
			if ($fields['wpcf-wills-and-expression-of-wish-forms']['file_data']['type'] != 'application/pdf'){ 
			 $errors['wills-and-expression-of-wish-forms']='only pdf format allowed';
			}
        }
    }
    return array($fields,$errors);
	
	
	add_filter('upload_mimes','add_custom_mime_types');
function add_custom_mime_types($mimes){
return array_merge($mimes,array (
     'pdf' => 'application/pdf'));
}
}

Could you please take a peek and let me know where I'm going wrong?
Thank you.

#2350449

Hello,

For the non-repeating file field, each file field should use different field slug, you can follow our document to setup the custom codes:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate
click link "Usage examples", you will see the example codes:

...
//For File and Image fields, $fields array also contain the upload data.
//For example, you can use the following code:
$fields['wpcf-my_file']['file_data'] = array(
    'size' => uploaded file size,
    'name' => name of uploaded file,
    'type' => type of uploaded file,
    'error' => what error occurred during upload,
    'tmp_name' => location of temporary uploaded file
);
...

In above codes, "wpcf-my_file" is the file field slug, you need to check those four single instance file field differently

#2350547

My issue is unresolved.