Uploaded files with required file fields not detected during validation
haykelbj - July 29, 2006 - 15:20
| Project: | Drupal |
| Version: | 4.7.6 |
| Component: | forms system |
| Category: | bug report |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | won't fix |
Jump to:
Description
When a form contains a file filed that is marked as required, the _form_validate() function always rises the error 'field is required' even if the field was not empty. I found that the value of $elements['#value'] for file fields is always empty during form validation in _form_validate(). If this is normal behavior, than a special test for file fields is needed. One solution could be to replace:
<?php
if ($elements['#required'] && empty($elements['#value']) && $elements['#value'] !== '0')
?>with
<?php
if ($elements['#required'] && (($elements['#type'] == 'file' && !file_check_upload($elements['#title']) || ($elements['#type'] != 'file' && empty($elements['#value']) && $elements['#value'] !== '0'))))
?>But if the element should anyway have value, than the error is somewhere else.

#1
In these cases could be provided a small test case for us: testers!
@haykelbj: could you chek if this still applies to HEAD? Thanks.
#2
The code still exists as stated. Must take another look next time, to confirm this problem.
#3
sorry, I'm not using drupal anymore but as I remember it should be easy to reproduce the problem: just create a form with a file field and mark it as required. Then use the form, fill the file field with a valid path and submit the form, you should still get the error 'field is required'. Good luck!
#4
I can confirm this still being a problem in 4.7.6 .
If you create a form with a file field set as required, the file upload is not being recognized.
The required setting being switched off makes the form behave as expected.
#5
This version is not supported. Reopen or create a new issue if the problem exists in any recent version (version equal or above Drupal 5)
#6
I posted a workaround here:
http://drupal.org/node/302138#comment-1497550