[Resolved] Validation custom field upload PDF edit form
This thread is resolved. Here is a description of the problem and solution.
Problem:
Validate custom upload fields in CRED. Solution:
In order for custom upload fields to be validated with CRED you will need to ensure that AJAX upload is enabled for this form. This means that you form uses the wordpress media gallery to upload its files.
Once this is enabled then the code below should allow you to validate your uploaded files.
// 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']==5050) || ($form_data['id']==846) )
{
if(isset($fields['wpcf-produktovy-list'])) {
//Retrieve file type
$file_type_uploaded=$fields['wpcf-produktovy-list']['field_data']['type'];
//Retrieve file size
$file_size_uploaded=$fields['wpcf-produktovy-list']['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['produktovy-list'] = 'Sorry the file you have uploaded is not of correct type (PDF) or exceeded 1MB limit';
}
}
}
//return result
return array($fields,$errors);
}
CRED FORM for logged users where i want to apply this validation - Edit form Řešení (ID: 5050)
Edit cred form settings:
Set this post status: Keep original status
Form submit: Submit this form without reloading the page (use AJAX)
After visitors Submit this form: Keep displaying form
I added this code to functions.php
// 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']==5050) || ($form_data['id']==846) )
{
if(isset($fields['wpcf-produktovy-list'])) {
//Retrieve file type
$file_type_uploaded=$fields['wpcf-produktovy-list']['field_data']['type'];
//Retrieve file size
$file_size_uploaded=$fields['wpcf-produktovy-list']['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['produktovy-list'] = 'Sorry the file you have uploaded is not of correct type (PDF) or exceeded 1MB limit';
}
}
}
//return result
return array($fields,$errors);
}
After some testing I've managed to find the correct solution and it was actually with the form settings.
So since the old AJAX upload was removed, the new AJAX upload is done using the media library for wordpress. So you code here.
// 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']==5050) || ($form_data['id']==846) )
{
if(isset($fields['wpcf-produktovy-list'])) {
//Retrieve file type
$file_type_uploaded=$fields['wpcf-produktovy-list']['field_data']['type'];
//Retrieve file size
$file_size_uploaded=$fields['wpcf-produktovy-list']['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['produktovy-list'] = 'Sorry the file you have uploaded is not of correct type (PDF) or exceeded 1MB limit';
}
}
}
//return result
return array($fields,$errors);
}
This is still valid, however to get it to work you need to enable "Use the WordPress Media Library manager for image, video, audio, or file fields " on your form.
Once you have done this your validation hook should start working.
thank you for finding solution, which is great, but i found very easy way how to "hack" this code :).
At the end your code works perfectlly - im not able UPLOAD other type than PDF - but im still able to SELECT image from previous uploaded image (from the media library) and save/sent this form with incorrect type of file.
For example:
I have got 3 different custom fields in CRED form:
1) for feature image
2) for gallery
3) for PDF
When i upload some new images into 1) or 2) field its possible to select one of this previous uploaded images and add into 3) field.
It is not neccesary function, but i will be nice to get rescriction to my users what type of files they will be able to upload into my website.
Thank you for your time and feel free to contact me anytime :).
I will be opening a ticket with our 2nd tier supporters for an improvement suggestion on this to help with the verification for previously uploaded files.
If there are no further questions on this then you can go ahead and mark this one as resolved.