Skip Navigation

[Resolved] cred_form_ajax_upload_validate – validate file extension

This support ticket is created 2 years, 11 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.

This topic contains 1 reply, has 1 voice.

Last updated by davidZ-4 2 years, 11 months ago.

Author
Posts
#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?
hidden link

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!

This ticket is now closed. If you're a Toolset client and need related help, please open a new support ticket.