Skip Navigation

[Resolved] restrict user to upload pdf file only

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

Problem: I would like to validate the file type of a file uploaded using CRED. Only PDFs should be allowed.

Solution:
Add the following code to functions.php:

function my_validation($field_data, $form_data)
{
    //field data are field values and errors
    list($fields,$errors)=$field_data;
 
    //validate if specific form
    if ($form_data['id']==138)
    {
 
        //check if uploaded file is right type
        if ($fields['wpcf-intern-cv']['field_data']['type'] != 'application/pdf')
        {
            //set error message for file
            $errors['intern-cv'] = 'Wrong file type';
        }
    }
 
    //return result
    return array($fields,$errors);
}
add_filter('cred_form_ajax_upload_validate','my_validation',10,2);

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/#cfauv

This support ticket is created 7 years, 6 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 3 replies, has 3 voices.

Last updated by SAUD BD 7 years, 5 months ago.

Assisted by: Christian Cox.

Author
Posts
#523925

I am trying to: prevent my form users from uploading their CV in a type other than pdf. Below my code. However when I test my form with doc file, the file is accepted and saved!

add_filter('cred_form_validate','cred_filetype_size_validation',10,2);
function cred_filetype_size_validation($field_data, $form_data)
{
// Field data are field values and errors
list($fields,$errors)=$field_data;

if (( 1174 == $form_data['id']) && (isset($_FILES['wpcf-intern-cv']['type'])) && (isset($_FILES['wpcf-intern-cv']['size']))) {

//Retrieve file type
$file_type_uploaded=$_FILES['wpcf-intern-cv']['type'];

//Retrieve file size
$file_size_uploaded=$_FILES['wpcf-intern-cv']['size'];

if (!( ('application/pdf' == $file_type_uploaded) && ($file_size_uploaded < 100000) )) {

$errors['intern-cv']='Sorry the file you have uploaded is not of correct type or exceeded 100kB limit.';

}

}
return array($fields,$errors);
}

Thank you.

#523980

Hi, since the file upload process has been changed to an AJAX upload, you must use the AJAX API function here:
https://toolset.com/documentation/programmer-reference/cred-api/#cfauv

Example for file size:

function my_validation($field_data, $form_data)
{
    //field data are field values and errors
    list($fields,$errors)=$field_data;

    //validate if specific form
    if ($form_data['id']==138)
    {

        //check if uploaded file is right type
        if ($fields['wpcf-intern-cv']['field_data']['type'] != 'application/pdf')
        {
            //set error message for file
            $errors['intern-cv'] = 'Wrong file type';
        }
    }

    //return result
    return array($fields,$errors);
}
add_filter('cred_form_ajax_upload_validate','my_validation',10,2);
#525557

Thanks, it works!

#588779

Hi,

I have two upload fields on CRED form, one for image file and one for pdf file type only!
When I tried the function above I could not get it to work to validate more than one field! but for single field it's working as expected!
Any suggestions!!

Thanks

function wpbd_member_form_validation ($error_fields, $form_data)
{
    //field data are field values and errors
	list($fields,$errors)=$error_fields;
    //validate if specific form
    if ($form_data['id'] == 163)
    {
        //check image size
        if ($fields['wpcf-picture']['field_data']['size'] > 85 * 1024) 
        {
            //set error message 
			$errors['wpcf-picture'] = 'Wrong image size!';
		}

		if ( ($fields['wpcf-member-documents']['field_data']['size'] > 55 * 1024) || ($fields['wpcf-member-documents']['field_data']['type'] != 'application/pdf') )
		{
			//set error message 
			$errors['wpcf-member-documents'] = 'Wrong size Or document type! only pdf allowed!';
		}
    }
	//return result
    return array($fields,$errors);
}
add_filter('cred_form_ajax_upload_validate','wpbd_member_form_validation',10,2);