Skip Navigation

[Gelöst] cred_form_ajax_upload_validate – validate file extension

This support ticket is created vor 3 Jahre, 1 Monat. 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.

Heute stehen keine Supporter zur Arbeit im Werkzeugsatz-Forum zur Verfügung. Sie können gern Tickets erstellen, die wir bearbeiten werden, sobald wir online sind. Vielen Dank für Ihr Verständnis.

Dieses Thema enthält 1 Antwort, hat 1 Stimme.

Zuletzt aktualisiert von davidZ-4 vor 3 Jahre, 1 Monat.

Author
Artikel
#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?
versteckter 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!

Dieses Ticket ist jetzt geschlossen. Wenn Sie ein Toolset Kunde sind und Hilfe benötigen, eröffnen Sie bitte ein neues Support-Ticket.