I am using a form to upload audio files to a repeatable field created within toolset and I want to validate the size.
I am using the example found below, for the cred_form_ajax_upload_validate api, but I cannot manage to get it to validate on repeatable fields.
https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_ajax_upload_validate
I also tried to create a new custom repeatable file field (and not audio) so I could manually validate the type as well as the size but cannot manage to make it work.
Any thoughts? Can you point me in the right direction?
Thank you!
[code]is this how it works?[/code]
Hello,
The filter hook cred_form_ajax_upload_validate works only you upload files using WP media library, you can edit your post form, enable option " Use the WordPress Media Library manager for image, video, audio, or file fields", then follow the document you mentioned above to setup the custom codes, for example this thread:
https://toolset.com/forums/topic/limit-size-of-uploaded-image-not-working/#post-1088293
Thank you for your answer.
Unfortunately, that code is not validating repeatable audio fields nor with repeating image fields nor single upload fields.
My two fields are wpcf-gallery and wpcf-_preview_filesvendors. I made sure I got my fields from the the Post Metadata.
Any thoughts?
Thanks for the feedback, the thread I mentioned above is just an example, you need to customize the codes according to your website settings, for example:
1) There is a custom repeating image field "gallery", which is created with Types plugin
2) You can setup your post form, enable option: Use the WordPress Media Library manager for image, video, audio, or file fields
3) Add below codes in your theme file "functions.php":
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']==123)
{
//get the latest image size
foreach($fields as $k => $v){
if(strpos($k, 'wpcf-gallery') !== false){
$size = $v['field_data']['size'];
if ($size > 1000000) // no more than 1M
{
$errors['wpcf-gallery'] = 'the image size should not be more than 1M bytes';
}
}
};
}
return array($fields,$errors);
}
Please replace 123 with your form's ID
Thank you, Luo! That worked great for the both fields.