Hi,
>Tell us what you are trying to do?
Validate files that people attach in post form. Specifically I want to validate file type to enable sending only PDF and only in Custom Field. It's not working or it blocks all files attached in custom field
>Is there any documentation that you are following?
I tried these 3 solutions from support:
https://toolset.com/forums/topic/validation-custom-field-upload-pdf-edit-form/
https://toolset.com/forums/topic/restrict-user-to-upload-pdf-file-only/
(These 2 doesn't work - people can send PDF but also images and other file types)
https://toolset.com/forums/topic/limit-file-types-to-upload/
(These one blocks everything - people can't send anything attached, even PDF)
On top of that I increased custom code snippet priority to 100 as recommended here:
https://toolset.com/forums/topic/custom-code-snippet-not-running/
I also put custom code in Toolset >> Settings >> Custom Code, not the other way.
I saved and tested custom code, I also activated these code snippets, one at a time. Seems like it should work, but it's not.
>Is there a similar example that we can see?
I found only those examples from links above
What can I do to make it work and validate only PDF files?
Hi Pawel,
Thank you for getting in touch.
Please try using the hook below.
add_filter('cred_form_validate','my_validation',10,2);
function my_validation($error_fields, $form_data)
{
//field data are field values and errors
list($fields,$errors)=$error_fields;
//uncomment this if you want to print the field values
//print_r($fields);
//validate if specific form
if ($form_data['id']==12)
{
//check my_field value
if ($fields['wpcf-my_field']['file_data']['type']!='pdf')
{
//set error message for my_field
$errors['my_field']='Not a PDF file';
}
}
//return result
return array($fields,$errors);
}
In this code above you will need to change the wpcf-my_field to the slug of your pdf field keeping the wpcf prefix. Also replace the form id 12 with the ID of your form.
Please let me know if this helps.
Thanks,
Shane
Hi Shane,
Thank you for your response.
I changed wpcf-my_field and $errors['my_field'] and id to match my form values.
It's still not accepting any files, even if it's PDF. I'm getting the same problem as I had with this code:
https://toolset.com/forums/topic/limit-file-types-to-upload/
What else can I do to solve this?
Hi Pawel,
We will perhaps need to do a dump of the file information to see what is happening.
Can you add this to your code and let me know what prints out on the screen after you've submitted the form with a PDF file.
var_dump($fields['wpcf-my_field']['file_data']['type']);
Thanks,
Shane