1

I just tried to create a simple mandatory file upload field in a custom form like this:

public function buildForm(array $form, FormStateInterface $form_state) {

  $form['#attributes'] = array('enctype' => "multipart/form-data");

  $form['pdf_file'] = array(
    '#type' => 'file',
    '#title' => $this->t('PDF File'),
    '#description' => $this->t('PDF files only'),
    //'#multiple' => TRUE,
    '#required' => TRUE,
  );

  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => $this->t('Save'),
  );

  return $form;
}

And that did not work.

When submitting a file (no matter what kind of), i get this error all the time:

"PDF File field is required."

Does anybody have an idea what I am doing wrong?

I am using Drupal 8.2

0

Try removing the required line. Updated code:

public function buildForm(array $form, FormStateInterface $form_state) {

  $form['#attributes'] = array('enctype' => "multipart/form-data");

  $form['pdf_file'] = array(
    '#type' => 'file',
    '#title' => $this->t('PDF File'),
    '#description' => $this->t('PDF files only'),
    //'#multiple' => TRUE,
  );

  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => $this->t('Save'),
  );

  return $form;
}

Update: As upload fields can not be required in PHP, just make it required in JS/jQuery

Add this code to a JS file:

$( "#target" ).submit(function( event ) {
  if( $(".pdf-file-field").files.length == 0 ){
    alert("Please add a file");
    event.preventDefault();
  }
});

Note: Instead of "event.preventDefault()", you should also be able to use return false.

  • Cannot. It is a mandatory field for the use case I am building. – Martin Dec 14 '16 at 8:45
  • Thanks a lot man! I did not even know that the #required flag is not usable for file upload fields. – Martin Dec 16 '16 at 8:18
  • @Martin If my answer solved your problem, could you accept my (answer)[stackoverflow.com/help/accepted-answer]? – Chris Happy Jan 30 '17 at 21:10
  • I solved it differently. I used the validate method inside the form class to ask if any file has been uploaded. Your solution is quite good for usuability. – Martin Feb 1 '17 at 8:16
0

Try using 'file_managed' instead of 'file':

namespace Drupal\mymodule\Form;

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;

/**
 * Class ExampleFileManagedForm.
 *
 * @package Drupal\mymodule\Form
 */
class ExampleFileManagedForm extends FormBase {


  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'example_file_managed_form';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {


    $form['pdf'] = array(
      '#type' => 'managed_file',
      '#title' => $this->t('Image'),
      '#upload_location' => 'public://pdf/',
      '#upload_validators' => array(
        'file_validate_extensions' => array('pdf'),
      ),
      '#required' => TRUE,
    );

    $form['submit'] = [
        '#type' => 'submit',
        '#value' => t('Submit'),
    ];

    return $form;
  }

  /**
    * {@inheritdoc}
    */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    parent::validateForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    dump($form_state->getValue('pdf')); die();

  }

}

This will output an array holding the newly created file id:

array:1 [▼
  0 => "467"
]

and I have tested that the required feature works as expected.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.