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:
if ($elements['#required'] && empty($elements['#value']) && $elements['#value'] !== '0')
with
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.
Comments
Comment #1
magico commentedIn these cases could be provided a small test case for us: testers!
@haykelbj: could you chek if this still applies to HEAD? Thanks.
Comment #2
magico commentedThe code still exists as stated. Must take another look next time, to confirm this problem.
Comment #3
haykelbj commentedsorry, 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!
Comment #4
cyvan commentedI 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.
Comment #5
pasqualleThis version is not supported. Reopen or create a new issue if the problem exists in any recent version (version equal or above Drupal 5)
Comment #6
keesje commentedI posted a workaround here:
http://drupal.org/node/302138#comment-1497550