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.
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