Tell us what you are trying to do?
I'm trying to limit the size of files that can be uploaded to a File type field using a CRED form.
Is there any documentation that you are following?
https://toolset.com/forums/topic/types-file-custom-field-and-cred/
I'm trying to use this post as a guide but it isn't working. The post is a bit old so I'm not sure if it should still work or if CRED has changed since.
What is the link to your site?
digitalmarketing.at - but you will need an account to see the site. I'll send you login details if required.
I've changed the code as in the instructions and added to my child theme functions.php:
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 (( 889 == $form_data['id'] ) && (isset($_FILES['wpcf-resource-file']['type'])) && (isset($_FILES['wpcf-resource-file']['size']))) {
//Retrieve file type
$file_type_uploaded=$_FILES['wpcf-resource-file']['type'];
//Retrieve file size
$file_size_uploaded=$_FILES['wpcf-resource-file']['size'];
//Validate files uploaded, make sure its PDF file type AND not more than 1MB
if (!( ('application/pdf' == $file_type_uploaded) && ($file_size_uploaded < 1048576) )) {
//Validation failed,
// set error message per field
//Remove wpcf - prefix here!
$errors['resource-file']='Sorry the file you have uploaded is not of correct type or exceeded 1MB limit.';
}
}
//return result
return array($fields,$errors);
}
889 is the ID of my cred form.
resource-file is the slug of the file upload field.
The file I'm testing the upload with is 3MB (versteckter Link)
Can you see why it doesn't have any effect?
Thanks
Hi, this code was written before CRED forms implemented AJAX file uploads by default. The cred_form_validate hook is invoked after images are uploaded, so it's not helpful for AJAX upload validation. To validate an upload file size when your form uses AJAX uploads, you can use the cred_form_ajax_upload_validate hook:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_ajax_upload_validate
You can click the orange "+ More" button in the API documentation to see an example.
Hi Christian
Thanks for your help. Getting there. I've added this code:
// For the resource file upload - check that the file is a PDF under 1MB in size.
add_filter('cred_form_ajax_upload_validate','cred_filetype_size_validation',10,2);
function cred_filetype_size_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']==889) || ($form_data['id']==846) )
{
//Retrieve file type
$file_type_uploaded=$fields['wpcf-resource-file']['field_data']['type'];
//Retrieve file size
$file_size_uploaded=$fields['wpcf-resource-file']['field_data']['size'];
//check if featured image exists
if ( ($file_size_uploaded > 1000000) || ($file_type_uploaded != 'application/pdf') )
{
//set error message for featured image
$errors['Resource File'] = 'Sorry the file you have uploaded is not of correct type (PDF) or exceeded 1MB limit';
}
}
//return result
return array($fields,$errors);
}
So the aim to have this working on 2 CRED forms 889 and 846.
I'm trying to only allow PDFs under 1MB.
This works for the wpcf-resource-file field, but it also seems to be running the same tests on the featured image field as well, which should allow image files.
I can see why, I think. When I upload a featured image it's triggering the cred_form_ajax_upload_validate hook, but because there is no entry in the wpcf-resource-file field it meets the ($file_type_uploaded != 'application/pdf) if statement so creates the error.
How do I separate this so it only acts on the correct field, or allows different tests for the different fields?
Thanks
You can use a conditional that tests whether or not the field key is set:
// For the resource file upload - check that the file is a PDF under 1MB in size.
add_filter('cred_form_ajax_upload_validate','cred_filetype_size_validation',10,2);
function cred_filetype_size_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']==889) || ($form_data['id']==846) )
{
if(isset($fields['wpcf-resource-file'])) {
//Retrieve file type
$file_type_uploaded=$fields['wpcf-resource-file']['field_data']['type'];
//Retrieve file size
$file_size_uploaded=$fields['wpcf-resource-file']['field_data']['size'];
//check if featured image exists
if ( ($file_size_uploaded > 1000000) || ($file_type_uploaded != 'application/pdf') )
{
//set error message for featured image
$errors['Resource File'] = 'Sorry the file you have uploaded is not of correct type (PDF) or exceeded 1MB limit';
}
}
}
//return result
return array($fields,$errors);
}
If you want to add more validation code for the featured image, you can add another if / isset statement that tests the _featured_image field.
Thanks Christian. All working now.