Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By yangli0516 on
I have this custom form which allows user to upload files. managed_file field is used for this purpose. Now I want to validate the uploaded file content (eg. check virus) after user submit the form. How do I clear the file field that it only display an empty file upload widget in the form after the validation fails?
Comments
Workaround?
As a workaround, I added another hidden manged_file field in the form. The purpose of this is to get empty file upload widget render array from $form variable. So when the validation fails, pass the hidden field value in $form variable to the field to clear.
This seems does the trick. But I am wondering if there's any other elegant ways.