Link to a page where the issue can be seen: hidden link
I expected to see: I expected the form to limit the size of images, but this does not seem to be happening. Can you see any problems with my code:
add_filter('cred_form_validate','cred_filetype_size_validation',10,2);
function cred_filetype_size_validation($field_data, $form_data)
{
// Field data are field values and errors
list($fields,$errors)=$field_data;
//Run the rest of code for this CRED ONLY and IF the file is upload type and size are set.
if (( 497 == $form_data['id']) && (isset($_FILES['wpcf-logo-profile-pic']['type'])) && (isset($_FILES['wpcf-logo-profile-pic']['size']))) {
//Retrieve file type
$file_type_uploaded=$_FILES['wpcf-logo-profile-pic']['type'];
//Retrieve file size
$file_size_uploaded=$_FILES['wpcf-logo-profile-pic']['size'];
//Validate files uploaded, make sure its jpg file type AND not more than 50kB
if (!( ('application/jpg' == $file_type_uploaded) && ($file_size_uploaded < 51200) )) {
//Validation failed,
// set error message per field
//Remove wpcf - prefix here!
$errors['logo-profile-pic']='Sorry the file you have uploaded is not of correct type (JPG) or exceeded filesize limit (50Kb).';
}
}
//Run the rest of code for this CRED ONLY and IF the file is upload type and size are set.
if (( 497 == $form_data['id']) && (isset($_FILES['wpcf-picture-1']['type'])) && (isset($_FILES['wpcf-picture-1']['size']))) {
//Retrieve file type
$file_type_uploaded=$_FILES['wpcf-picture-1']['type'];
//Retrieve file size
$file_size_uploaded=$_FILES['wpcf-picture-1']['size'];
//Validate files uploaded, make sure its jpg file type AND not more than 500kB
if (!( ('application/jpg' == $file_type_uploaded) && ($file_size_uploaded < 512000) )) {
//Validation failed,
// set error message per field
//Remove wpcf - prefix here!
$errors['picture-1']='Sorry the file you have uploaded is not of correct type (JPG) or exceeded filesize limit (500Kb).';
}
}
//Run the rest of code for this CRED ONLY and IF the file is upload type and size are set.
if (( 497 == $form_data['id']) && (isset($_FILES['wpcf-picture-2']['type'])) && (isset($_FILES['wpcf-picture-2']['size']))) {
//Retrieve file type
$file_type_uploaded=$_FILES['wpcf-picture-2']['type'];
//Retrieve file size
$file_size_uploaded=$_FILES['wpcf-picture-2']['size'];
//Validate files uploaded, make sure its jpg file type AND not more than 500kB
if (!( ('application/jpg' == $file_type_uploaded) && ($file_size_uploaded < 512000) )) {
//Validation failed,
// set error message per field
//Remove wpcf - prefix here!
$errors['picture-2']='Sorry the file you have uploaded is not of correct type (JPG) or exceeded filesize limit (500Kb).';
}
}
//Run the rest of code for this CRED ONLY and IF the file is upload type and size are set.
if (( 497 == $form_data['id']) && (isset($_FILES['wpcf-picture-3']['type'])) && (isset($_FILES['wpcf-picture-3']['size']))) {
//Retrieve file type
$file_type_uploaded=$_FILES['wpcf-picture-3']['type'];
//Retrieve file size
$file_size_uploaded=$_FILES['wpcf-picture-3']['size'];
//Validate files uploaded, make sure its jpg file type AND not more than 500kB
if (!( ('application/jpg' == $file_type_uploaded) && ($file_size_uploaded < 512000) )) {
//Validation failed,
// set error message per field
//Remove wpcf - prefix here!
$errors['picture-3']='Sorry the file you have uploaded is not of correct type (JPG) or exceeded filesize limit (500Kb).';
}
}
//return result
return array($fields,$errors);
}
Hello. Thank you for contacting the Toolset support.
CRED now upload the file using AJAX so there is different hook to validate the file size: cred_form_ajax_upload_validate
For example:
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']==9999)
{
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);
}
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']==497)
{
if ($fields['logo-profile-pic']['field_data']['size'] > 51200)
{
//set error message for featured image
$errors['logo-profile-pic'] = 'Profile Pic too large (>50Kb)';
}
}
//return result
return array($fields,$errors);
}
I've changed the Form ID and the field name to logo-profile-pic. Can you see anything I've done wrong?
Could you please try following code: I've added prefix "wpcf-" to your custom field name.
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']==497)
{
if ($fields['wpcf-logo-profile-pic']['field_data']['size'] > 51200)
{
//set error message for featured image
$errors['wpcf-logo-profile-pic'] = 'Profile Pic too large (>50Kb)';
}
}
//return result
return array($fields,$errors);
}
There is no feature available to display only error message but I'll pass your concern to our feature request team and send it as a new feature request for assessment.