Set minimum files to upload
-
Hi guys, in my form I need from client to upload at least 3 files mandatory, is something like this possible to achieve?
Thank you
-
Hi @9qwer9,
At the moment, I’m afraid there isn’t any out of the box setting to set a limit on minimum files that could be uploaded.
I suppose you are referring to the “Multiple” type in “File Upload”? The field only provides a setting to keep a limit on a maximum number of files.
A workaround would be to use the “Field Group” field by setting the value for “Minimum repeater limit” to 3 and then add a Single “File Upload” field inside it so that it would display 3 upload fields by default.
Screenshot at 17:08:15.png
Screenshot at 17:08:35.png
Please do check and see whether the above helps.
Best Regards,
Nithin
Hi Nithin, thanks for this, unfortunately this doesn’t look ok from the end-user perspective when I checked that on the frontend. Isn’t there any other possibility of custom validation of this field?
Thank you!Hi @9qwer9
Thanks for response!
The solution suggested is the simplest one and the only one currently possible “out of the box”.
It may also be possible to do this with some sort of custom validation but it would require additional custom code.
I have already asked our developers about it and I’m waiting for their response. They’ll look into it and see if we could provide such code so please keep an eye on this topic for further updates. I would appreciate some patience, though, as our developers are dealing with a lot of complex tasks on daily basis and their response time may be a bit longer than ours here.
Best regards,
AdamHi @9qwer9
Meanwhile I gave it another look and I came up with this little code that should do the trick for your:
<?php add_filter( 'forminator_custom_form_submit_errors', 'frmt_upload_low_limit', 10, 3 ); function frmt_upload_low_limit( $submit_errors, $form_id, $field_data_array ) { $form_ids = array( 2688, 123 ); // form IDs - one or more; if more - separate numbers with commas $fields_ids = array( 'upload-1' ); // IDs of upload fields to check, one or more, if more - separate with commas $low_limit = 3; // minimum number of files required $err_msg = 'You need to upload at least ' . $low_limit . ' files!'; // custom error message if ( !in_array( $form_id, $form_ids ) ) { return $submit_errors; // just bail out and skip checks } foreach( $field_data_array as $key => $value ) { $field_name = $value['name']; if ( in_array( $field_name, $fields_ids ) ) { $submitted_files = count($value['value']['file']); if ( $submitted_files < $low_limit ) { $submit_errors[][$field_name] = $err_msg; } } } return $submit_errors; }To use it on site first you need to create an empty file with . php extension (e.g. “forminator-upload-minimum-file-limit.php” or similar) and copy-paste code into that file.
Then you need to “configure” the code in these lines
$form_ids = array( 2688, 123 ); // form IDs - one or more; if more - separate numbers with commas $fields_ids = array( 'upload-1' ); // IDs of upload fields to check, one or more, if more - separate with commas $low_limit = 3; // minimum number of files required $err_msg = 'You need to upload at least ' . $low_limit . ' files!'; // custom error messageas described in code comments; I think they are pretty self-explanatory.
Then save the file and upload it to the /wp-content/mu-plugins folder of your site’s WordPress install.
Finally, you need to make sure that your upload field is set to be required on the form as the code will cover cases of less then required files added but will accept empty upload if field is not required. Once the field is set to required – this case will also be addressed.
Best regards,
AdamHi Adam!
Thank you very much, it is working perfectly!
It is just another reminder for me to work on my coding skills 🙂
Thanks again, I owe you this one 🙂Man, I dont know what happened, but I migrated my site to another domain an it stopped working.. When I set minimum to 5, and I upload 5 files, it throws error, you have to upload at least 5 files.. even when 5 are already there
Hi @9qwer9,
I tested the snippet in my system and can confirm it works when tested. Could you please confirm whether form ID, Field ID(upload-1) hasn’t changed in the code you have implemented?
If you can confirm they are correct, then possible to share the form export so that we could check how the form behaves on our side?
Please check the following doc on how to export a form:
https://wpmudev.com/docs/wpmu-dev-plugins/forminator/#import-exportIf you are concerned about any sensitive information in the form, then you can duplicate your form, remove any sensitive information, and then export it.
You can share the export file via Google Drive, Dropbox or any cloud services in the next reply.
Looking forward to your response.
Kind Regards,
Nithin
Hello,
I double checked IDs of form and field and they are the same, when I have selected limit to 3 it is ok, but when I change it to 5 then it is problem.
Here is a link
form
Thank you for checking it!Hi @9qwer9
Thanks for sharing the form.
I did some more testing and it seems you are right. It’s actually an interesting coincidence – it we used a higher number than 3 from the start, we’d see the issue right away.
The key point here is (I’m sorry, I didn’t notice that earlier) that depending on the form and upload fields settings (related to AJAX mostly, if I correctly tested it), the data sent in submission for uploaded files has slightly different structure. That “confuses” code and in some cases it simply counts incorrect data.
I did a small update and it seems to be fine in my tests now so please give it a go instead of previous version:
add_filter( 'forminator_custom_form_submit_errors', 'frmt_upload_low_limit', 10, 3 ); function frmt_upload_low_limit( $submit_errors, $form_id, $field_data_array ) { $form_ids = array(18); // form IDs - one or more; if more - separate numbers with commas $fields_ids = array( 'upload-1' ); // IDs of upload fields to check, one or more, if more - separate with commas $low_limit = 5; // minimum number of files required $err_msg = 'You need to upload at least ' . $low_limit . ' files!'; // custom error message if ( !in_array( $form_id, $form_ids ) ) { return $submit_errors; // just bail out and skip checks } $submitted_files = 0; foreach( $field_data_array as $key => $value ) { $field_name = $value['name']; if ( in_array( $field_name, $fields_ids ) ) { $uploaded_files = $value['value']['file']; if ( array_key_exists( 'file_url', $uploaded_files ) ) { $submitted_files = count($uploaded_files['file_url']); } else { $submitted_files = count($uploaded_files); } if ( $submitted_files < $low_limit ) { $submit_errors[][$field_name] = $err_msg; } } } return $submit_errors; }Best regards,
AdamHi,
sorry for delayed reply, but now it is working as expected.
Thank you very much for your time and effort.
Best regards
RichardHi @9qwer9,
We are happy to hear that the issue has been resolved and marking this thread accordingly.
Please let us know in case you need further help.Kind regards,
Zafer
The topic ‘Set minimum files to upload’ is closed to new replies.