0

I have a custom form created with Form API. In this form, I have a file field with the type managed_file

$form['fichiers_justificatif'] = array(
    '#type' => 'managed_file',
    '#title' => 'justificatifs',
    '#upload_validators' => $validators,
    '#upload_location' => 'private://justificatifs',
    '#multiple' => TRUE,
    '#description' => 'types de fichier autorisés : png gif jpg jpeg pdf doc',
);

It works well, I can upload a file, get its name and url.

But I don’t know how to have a multiple file upload like the one with see in admin or with the webform module.

Example of webform module which handle multiple files upload :

enter image description here

I’ve tried to add '#multiple' => TRUE, but I have an Ajax error after the 2nd file is submitted.

I’ve tried this module Multi-value form element but still have AJAX issue.

I’ve tried dropzone.js but I'm not satisfied with it since it adds an other interface for file (different from the one in admin). User are confused there is an other interface for files.

What could be the better way to handle this ?

4
  • Does this answer your question? Some #states not working for custom field but do work on identical webform field - This question does not directly answer how to upload files, but it shows you how to use the custom, more advanced FormAPI Elements from Webform module.
    – Hudri
    Commented Jan 12, 2021 at 10:10
  • 1
    #multiple works, at least for these 6 people who, like me, upvoted this answer drupal.stackexchange.com/a/226326/47547. Could be a conflict with other Ajax code on your site.
    – 4uk4
    Commented Jan 12, 2021 at 10:10
  • @4k4 thank you for confirming me that #multipleworks. I will try to debug my AJAX issue then. In my form, I uses some custom services and I see in the Ajax error that the 2nd file cause an Ajax call to those services which initially works. My issue seems to be related to this issue. Commented Jan 12, 2021 at 10:18
  • @4k4 So since you confirm me that #multiple works, I was able to debug the Ajax issue. The answer that helps me : drupal.stackexchange.com/questions/212928/… (use protected instead of private for my custom services). Commented Jan 12, 2021 at 10:36

0

Browse other questions tagged or ask your own question.