[Resolved] Display error if sum of all file sizes, repeated field, cred_form_validate
This support ticket is created 6 years, 11 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.
No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.
I am trying to calculate the total of all uploaded files and display an error if it is more than a specified value.
The problem is that I can't get any values. I tried to follow your documentation for cred_form_validate.
I have this
unction email_save_maxsize_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']==22263 || $form_data['id']==22257)
{
error_log('Form id ok');
error_log('fields: '.print_r($fields));
$array_size = $fields['wpcf-attachment']['file_data']['size'];
error_log('array_size: '.print_r($array_size));
$max_total = 0;
foreach($array_size as $size) {
$max_total = $max_total + $size;
error_log('Size: '.$size);
}
if ($max_total > 3000000) {
$errors['wpcf-attachment'] = 'Dina bilagors sammanlagda storlek överstiger tillåtet värde – 3MB. Dina bilagor är: '.round($max_total/1000000, 2).'MB tillsammans.';
}
}
//return result
return array($fields,$errors);
}
add_filter('cred_form_validate','email_save_maxsize_validation', 10, 2);
from error_log:
[12-Dec-2017 10:15:00 UTC] Form id ok
[12-Dec-2017 10:15:00 UTC] fields: 1
[12-Dec-2017 10:15:00 UTC] array_size: 1
At least on my test site the line $array_size = $fields['wpcf-attachment']['file_data']['size']; doesn't return anything meaningful because the array field wpcf-attachment doesn't contain and entry for file_data.
Because the files are pre-loaded via ajax before you submit the form the global $_FILES variable can't help you out.
You will have to use the PHP filesize function (hidden link) to get the sizes of the already uploaded files and check.
Note that these files already uploaded by CRED, if they are too large you will need to take care of deleting them yourself.
It has been discussed with the developers before about automatically deleting files uploaded as part of a failed form submission and that has been rejected, so your validation routine—if it rejects the files as being too large—will also need to delete them.