Skip Navigation

[Résolu] cred_form_ajax_upload_validate – validate file extension

This support ticket is created Il y a 3 années et 4 mois. 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.

Aucun de nos assistants n'est disponible aujourd'hui sur le forum Jeu d'outils. Veuillez créer un ticket, et nous nous le traiterons dès notre prochaine connexion. Merci de votre compréhension.

Ce sujet contient 1 réponse, a 1 voix.

Dernière mise à jour par davidZ-4 Il y a 3 années et 4 mois.

Auteur
Publications
#2114571

Tell us what you are trying to do? validate file extension during upload and restrict acceptable file type to csv, xls, and xlsx

Is there any documentation that you are following?
https://toolset.com/errata/a-note-on-using-the-cred_form_ajax_upload_validate-hook/
https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_ajax_upload_validate

Is there a similar example that we can see?

What is the link to your site?
lien caché

I wrote the following function but the files i can upload to the custom field can be any type.

add_filter('cred_form_ajax_upload_validate', 'y2risk_validate_portfolio_file_format', 10, 2 );
function y2risk_validate_portfolio_file_format( $error_fields, $form_data ) {
		// Get the list of uploading fields and errors
	list( $fields, $errors ) = $error_fields;
	// if the form is 881 or 386
	if ( $form_data['id'] == 881 || $form_data['id'] == 386 ) {
		$uploaded_file_format = $fields['wpcf-u-sngl-portfolio-file']['field_data']['type'];
		$accepted_file_formats = array(
			'text/csv', // csv
			'application/vnd.ms-excel', // xls
			'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' // xlsx
		);
		if ( ! in_array( $uploaded_file_format, $accepted_file_formats ) ) {
			$errors['File-Format-Error'] = 'The file is not an accepted format. Accepted formats: .csv, .xls, .xlsx';
		}
	}
	return array( $fields, $errors );
}

for some reason when I upload a file type other than the one I want the error does not display and the file type is accepted.
Please help,
Thanks,

David

#2114741

My issue is resolved now. Thank you!