You are here

public function FileEditForm::validateForm in File Entity (fieldable files) 8.2

Button-level validation handlers are highly discouraged for entity forms, as they will prevent entity validation from running. If the entity is going to be saved during the form submission, this method should be manually invoked from the button-level validation handler, otherwise an exception will be thrown.

Overrides ContentEntityForm::validateForm

File

src/Form/FileEditForm.php, line 137

Class

FileEditForm
Form controller for file type forms.

Namespace

Drupal\file_entity\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {

  // Handle the replacement file if uploaded.
  if ($form_state
    ->getValue('replace_upload')) {

    // Save the file as a temporary file.
    $file = file_save_upload('replace_upload', $form['replace_upload']['#upload_validators']);
    if (!empty($file)) {

      // Put the temporary file in form_state so we can save it on submit.
      $form_state
        ->setValue('replace_upload', $file);
    }
    elseif ($file === FALSE) {

      // File uploaded failed.
      $form_state
        ->setError($form['replace_upload'], t('The replacement file could not be uploaded.'));
    }
  }
  parent::validateForm($form, $form_state);
}