Skip Navigation

[Closed] PDF validation in Post Form

This support ticket is created 3 years, 10 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 3 replies, has 2 voices.

Last updated by Shane 3 years, 10 months ago.

Assisted by: Shane.

Author
Posts
#2328917

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?

#2328991

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

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

#2330791

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?

#2330857

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

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

The topic ‘[Closed] PDF validation in Post Form’ is closed to new replies.