Skip Navigation

[Resolved] How to limit the file upload size limit from the CRED form?

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

This support ticket is created 5 years, 10 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)

This topic contains 6 replies, has 2 voices.

Last updated by wilfredC 5 years, 9 months ago.

Assisted by: Luo Yang.

Author
Posts
#1208829

This is the code I use and it didn't work:

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;

//Run the rest of code for this CRED ONLY and IF the file is upload type and size are set.
if (( 3850 == $form_data['id']) && (isset($_FILES['wpcf-addition-file']['size']))) {

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

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

//Validate files uploaded, make sure its PDF file type AND not more than 100kB
if (!$file_size_uploaded < 100) {

//Validation failed,
// set error message per field
//Remove wpcf - prefix here!
$errors['addition-file']='Sorry the file you have uploaded is not of correct type or exceeded size limit.';

}

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

------------
3850 is the form ID and "addition-file" is the slug of the custom field

#1209233

Hello,

I assume you are using default AJAX upload feature in your post form, if it is, please try another filter hook "cred_form_ajax_upload_validate":

https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_ajax_upload_validate
This hook provides custom validation for files that are uploaded using AJAX

You can get the example codes by clicking link "Usage examples"

#1209241

Hi I change the code to this but still not working, it still can upload the file size more than 100:

add_filter('cred_form_ajax_upload_validate','my_validation',10,2);
function my_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']== 3850)
{
//check if featured image exists o
if ($fields['wpcf-addition-file']['size'] > 100)
{
//set error message for featured image
$errors['addition-file'] = 'Wrong size image';
}
}
//return result
return array($fields,$errors);
}

PS: the field type I use is File

#1209307

Hello,

Please try to modify your PHP codes as below:


add_filter('cred_form_ajax_upload_validate','my_validation',10,2);
function my_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']== 20)
	{
		//check if featured image exists o
		if ($fields['wpcf-addition-file']['field_data']['size'] > 100000)
		{
			//set error message for featured image
			$errors['addition-file'] = 'Wrong size image';
		}
	}
	//return result
	return array($fields,$errors);
}

And test again

#1209321
remove.JPG

Hi,
It solved the problem, but one more thing.
How can I remove the "addition-file" text in the alert message "addition-file: Wrong size image"?

#1209326

It is not recommended to remove it, but you can change the text, for example, change this line from:

$errors['addition-file'] = 'Wrong size image';

To:

$errors['error'] = 'Wrong size image';
#1209863

My issue is resolved now. Thank you!