File Upload Field not working 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' =&gt; "multipart/form-data");

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

  $form['submit'] = array(
    '#type' =&gt; 'submit',
    '#value' =&gt; $this-&gt;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
Try removing the required line. Updated code:

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

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

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

  $form['submit'] = array(
    '#type' =&gt; 'submit',
    '#value' =&gt; $this-&gt;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.