Startseite › Toolset Professional Support › [Gelöst] cred_form_validate filer doesn't work for CRED forms
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.
Sun | Mon | Tue | Wed | Thu | Fri | Sat |
---|---|---|---|---|---|---|
- | 10:00 – 13:00 | 10:00 – 13:00 | 10:00 – 13:00 | 10:00 – 13:00 | 10: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/Kolkata (GMT+05:30)
Dieses Thema enthält 19 Antworten, hat 3 Stimmen.
Zuletzt aktualisiert von cassianS vor 1 Jahr, 8 Monaten.
Assistiert von: Minesh.
Tell us what you are trying to do?
- I am trying to validate CRED forms to limit files upload to specific file types of limited size.
I created a custom code snippet with the following code below and it simply doesn't work: nothing happens, no errors in logs etc.
Not sure what a I doing wrong.
add_filter('cred_form_validate','cred_filetype_size_validation',10,2);
function cred_filetype_size_validation($field_data, $form_data)
{
if ( ( (251 == $form_data['id']) OR (721 == $form_data['id']) ) AND (isset($_FILES['wpcf-scan-image-file']['value']['type'])) AND (isset($_FILES['wpcf-scan-image-file']['value']['size'])) AND ($_FILES['wpcf-scan-image-file']['value']['size'])>0 ) {
$file_type_uploaded=$_FILES['wpcf-scan-image-file']['value']['type'];
$file_size_uploaded=$_FILES['wpcf-scan-image-file']['value']['size'];
if (!( ( ('application/pdf' == $file_type_uploaded) ||
('application/zip' == $file_type_uploaded) ||
('image/jpeg' == $file_type_uploaded) ||
('image/jpg' == $file_type_uploaded) ||
('image/tiff' == $file_type_uploaded) ||
('image/png' == $file_type_uploaded) ||
('application/dicom' == $file_type_uploaded) ) && ($file_size_uploaded < 600000000) )) {
$errors['scan-image-file']='Sorry the file you have uploaded is not of the correct type or exceeded 600MB limit.';
return array($fields,$errors);
}
}
}
Is there any documentation that you are following?
- I followed this:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate
https://toolset.com/forums/topic/cred-1-4-file-type-and-file-size-validation-at-submission/
What is the link to your site?
- versteckter Link
Hello. Thank you for contacting the Toolset support.
What if you try to use the following modified hook code. You can add the following modified code to "Custom Code" section offered by Toolset:
- https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/#adding-custom-php-code-using-toolset
add_filter('cred_form_validate','cred_filetype_size_validation',10,2); function cred_filetype_size_validation($field_data, $form_data) { list($fields,$errors)=$error_fields; if ( ( (251 == $form_data['id']) OR (721 == $form_data['id']) ) AND (isset($fields['wpcf-scan-image-file']['field_data']['type'])) AND (isset($fields['wpcf-scan-image-file']['field_data']['size'])) AND ($fields['wpcf-scan-image-file']['field_data']['size'])>0 ) { $file_type_uploaded=$fields['wpcf-scan-image-file']['field_data']['type']; $file_size_uploaded=$fields['wpcf-scan-image-file']['field_data']['size']; if (!( ( ('application/pdf' == $file_type_uploaded) || ('application/zip' == $file_type_uploaded) || ('image/jpeg' == $file_type_uploaded) || ('image/jpg' == $file_type_uploaded) || ('image/tiff' == $file_type_uploaded) || ('image/png' == $file_type_uploaded) || ('application/dicom' == $file_type_uploaded) ) && ($file_size_uploaded < 600000000) )) { $errors['wpcf-scan-image-file']='Sorry the file you have uploaded is not of the correct type or exceeded 600MB limit.'; return array($fields,$errors); } } }
Do you see it working?
Hi Minesh,
thank you for your reply.
I added your code but it doesn't make any change either... I am still able to submit other than the requested files using specified forms.
Can you please share problem URL where you added your form as well as admin access details.
*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.
I have set the next reply to private which means only you and I have access to it.
When I try to access the problem URL you shared where you added your form:
- versteckter Link
I found that the image field is the repeating (multiple instance) field. So the field "scan-image-file" will have more than one instance to validate.
To validate multiple instance field you will have to loop through the all instances. Here is the reference ticket you may like to review:
- https://toolset.com/forums/topic/trying-to-validate-non-repeating-image-fields-on-front-end-user-form/#post-2334355
Here is the adjusted code - feel free to adjust it if required:
add_filter('cred_form_validate','cred_filetype_size_validation',10,2); function cred_filetype_size_validation($field_data, $form_data) { list($fields,$errors)=$error_fields; // target form IDs $forms = array( 251 , 721 ); if (in_array($form_data['id'], $forms ) ) { // target form field $form_fields = array("wpcf-scan-image-file"); // allowed image sizes $file_types = array('image/jpeg', 'image/png' , 'image/jpg' , 'application/pdf', 'application/zip', 'image/tiff', 'application/dicom'); // allowed size $max_size = 600000000; foreach($form_fields as $k=>$v): $type = $_FILES[$v]['type']; $size = $_FILES[$v]['size']; if ( (!empty($type) and !in_array( $type, $file_types )) or (!empty($size) and $size >= $max_size ) ) { $errors[$v] = 'Sorry the file you have uploaded is not of the correct type or exceeded 600MB limit.'; } endforeach; } return array($fields,$errors); }
Can you please try to use the above validation hook and check if that help you to resolve your issue.
Thank you, Minesh.
The code you shared works with an error: it gives an error message with every submission, no matter which file type I use.
In PHP log I see the following PHP notice after form submissions:
"PHP Notice: Undefined variable: error_fields in /wp-content/toolset-customizations/validate-cred-file-uploads-order-report-forms.php on line 14"
There was a type in the code. Can you please try to use the following code:
add_filter('cred_form_validate','cred_filetype_size_validation',10,2); function cred_filetype_size_validation($error_fields, $form_data) { list($fields,$errors)=$error_fields; // target form IDs $forms = array( 251 , 721 ); if (in_array($form_data['id'], $forms ) ) { // target form field $form_fields = array("wpcf-scan-image-file"); // allowed image sizes $file_types = array('image/jpeg', 'image/png' , 'image/jpg' , 'application/pdf', 'application/zip', 'image/tiff', 'application/dicom'); // allowed size $max_size = 600000000; foreach($form_fields as $k=>$v): $type = $_FILES[$v]['type']; $size = $_FILES[$v]['size']; if ( (!empty($type) and !in_array( $type, $file_types )) or (!empty($size) and $size >= $max_size ) ) { $errors[$v] = 'Sorry the file you have uploaded is not of the correct type or exceeded 600MB limit.'; } endforeach; } return array($fields,$errors); }
Can you please confirm the solution I shared with my previous reply help you to resolve your issue.
Per your request:
https://toolset.com/forums/topic/cred_form_validate-filer-doesnt-work-for-cred-forms-2/
I have reopen this ticket, please update here if you still need assistance for it
Thank you.
Sorry, for the late response, Minesh. I thought I replied to you but looks like I did not reply to your last message.
I tested the code you kindly advised. Unfortunately, it works not correctly: no matter what file I upload it gives an error message that the file type or size is not right.
I've adjusted the code added to the "Custom Code" section as given under:
// -- CRED Validate Upload File Size and Type------------- add_filter('cred_form_validate','cred_filetype_size_validation',10,2); function cred_filetype_size_validation($error_fields, $form_data) { list($fields,$errors)=$error_fields; // target form IDs $forms = array( 251 , 721 ); if (in_array($form_data['id'], $forms ) ) { // target form field $form_fields = array("wpcf-scan-image-file"); // allowed image sizes $file_types = array('image/jpeg', 'image/png' , 'image/jpg' , 'application/pdf', 'application/zip', 'image/tiff', 'application/dicom'); // allowed size $max_size = 600000000; foreach($form_fields as $k=>$v): $count = count($_FILES[$v]['type']); for($i=0;$i<$count;$i++) { $type = $_FILES[$v]['type'][$i]; $size = $_FILES[$v]['size'][$i]; if ( (!in_array( $type, $file_types )) or ( $size >= $max_size ) ) { $errors[$v] = 'Sorry the file you have uploaded is not of the correct type or exceeded 600MB limit.'; } } endforeach; } return array($fields,$errors); }
Can you please confirm it works as expected now.
Hi Minesh, thank you again!
I made tests and the previous issue is gone.
However, there are couple of other things not right:
- for some reason, I get an error when upload .zip file.
- also, when I try to upload DICOM (.dcm) I get another error right after adding the file (before submission). Here is the HTML element with an error message:
<small for="cred_form_721_1_1_scan-image-file" generated="true" class="wpt-form-error">This field is required.</small>
I checked that user roles that submit these forms with files has access to "publish and edit own" media files, not only read them.
When I deactivate access plugin, this issue gone. I am not sure what settings would be right to allow uploads of .dmc files...
Can you please check now.
To allow the .dcm file upload I've added the following filter to the "Custom Code" section where we added the validation hook:
add_filter( 'upload_mimes', 'func_allow_dcm_file_uploads' ); function func_allow_dcm_file_uploads( $mimes ) { $mimes['dcm'] = 'image/dicom'; return $mimes; }
I've also adjusted the related validation hook to allow zip files.
Now, if it still does not work you will have to send me sample .zip and .dcm files you are trying to upload and I'll check further whats going wrong with the files you are trying to upload.
Hi Minesh.
Thank you so much.
There is no issue now with .zip files.
However, I still experience issues with .dcm files.
I use sample files for tests from these sites:
versteckter Link
and versteckter Link
Can you please check now. I have tried to upload the .dcm file and for the .dcm file the file type is: application/octet-stream
so, I've adjusted the code for allowed file types and added the 'application/octet-stream'
$file_types = array('image/jpeg', 'image/png' , 'image/jpg' , 'application/pdf', 'application/zip', 'image/tiff', 'application/dicom', 'application/dcm', 'image/dcm', 'image/dicom', 'application/x-zip-compressed','application/octet-stream');
Can you please confirm it works as expected now.