Required flag on file forms breaks on validation

Created on 21 April 2006, about 18 years ago
Updated 10 October 2023, 9 months ago

Problem/Motivation

A form with a '#type' => 'file' element that is required will always fail. Since there is no #value set (theoretically, there is no value at all) the validation will raise an error.

Steps to reproduce

1) Create a Form class
2) Add

 $form['file_test_upload'] = [
      '#type' => 'file',
      '#title' => t('Upload a file'),
      '#required' => TRUE,
    ];

to your buildForm method
3) Go to the form
4) Add a file to the field and submit it.
5) Notice the form are not able to be submitted because the file field is required.

Proposed resolution

Add a valueCallback on \Drupal\Core\Render\Element\File.

Remaining tasks


Manual testing
Review
Commit

User interface changes

API changes

Elements of #type file now have a #value. This means that using $form_state->getValue('some_file_field') will return an array of \Symfony\Component\HttpFoundation\File\UploadedFile objects

Data model changes

Release notes snippet

🐛 Bug report
Status

Fixed

Version

9.5

Component
File system 

Last updated about 18 hours ago

Created by

🇳🇱Netherlands Bèr Kessels

Live updates comments and jobs are added and updated live.

Missing content requested by

🇦🇺Australia dpi
7 months ago
Sign in to follow issues

Comments & Activities

Production build 0.69.0 2024