Skip Navigation

[Resolved] Custom file type validation with generic field

This thread is resolved. Here is a description of the problem and solution.

Problem:

I am using a generic field for our form as a file input but I want to validate its file type, size, and extension.

Solution:

You might consider custom codes, for example:

https://toolset.com/forums/topic/custom-file-type-validation-with-generic-field/#post-1557031

Relevant Documentation:

https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_ajax_upload_validate

This support ticket is created 4 years, 8 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 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

Tagged: 

This topic contains 2 replies, has 2 voices.

Last updated by Nicholas 4 years, 8 months ago.

Assisted by: Luo Yang.

Author
Posts
#1556789
image_2020_03_19T13_19_28_922Z.png

I am using a generic field for our form as a file input but I want to validate its file type, size, and extension. I tried to pass a validate attribute but it's not working. The shortcode looks like this. Can you please help us to pass the validation

[cred_generic_field type='file' field='charity-campaign-image' class='charity-campaign-image']
{
"required":0,
}
[/cred_generic_field]

If you check the screenshot then there you'll see data attributes for different validations as well as for file size, file extension, file type, and as I checked in the code there is some code which is getting the attribute passed with a generic field parameter but somehow when I pass something like that for validation that's not working and same output is coming instead of my limited file extension validation for i.e I want to allow only jpg, jpeg, png, etc

#1557031

Hello,

There isn't such kind of built-in feature within shortcode, see our document:
https://toolset.com/documentation/user-guides/front-end-forms/cred-shortcodes/#cred_generic_field

In your case, you might consider custom codes, for example:
1) If you are using option "Use the WordPress Media Library manager for image, video, audio, or file fields" in the post/user form, you can use filter hook "cred_form_ajax_upload_validate" to setup the file extension validation, see our document:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_ajax_upload_validate

2) If you are not using above option, you can try filter hook "cred_form_validate", see our document:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate

More help:
Using the Native WordPress Media Manager in Front-End Forms
https://toolset.com/documentation/user-guides/front-end-forms/using-the-native-wordpress-media-manager-in-front-end-forms/

#1559135

My issue is resolved now. Thank you!