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);
}
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);
}
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
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);
}