You are here

public static function Upload::validate in Lightning Media 8.4

Same name and namespace in other branches
  1. 8 src/Element/Upload.php \Drupal\lightning_media\Element\Upload::validate()
  2. 8.2 src/Element/Upload.php \Drupal\lightning_media\Element\Upload::validate()
  3. 8.3 src/Element/Upload.php \Drupal\lightning_media\Element\Upload::validate()

Validates the uploaded file.

Parameters

array $element: The element.

\Drupal\Core\Form\FormStateInterface $form_state: The current form state.

File

src/Element/Upload.php, line 41

Class

Upload
A form element to handle file uploads.

Namespace

Drupal\lightning_media\Element

Code

public static function validate(array &$element, FormStateInterface $form_state) {
  if ($element['#value']) {
    $file = File::load($element['#value']);
    $errors = file_validate($file, $element['#upload_validators']);
    if ($errors) {
      foreach ($errors as $error) {
        $form_state
          ->setError($element, $error);
      }
      static::delete($element);
    }
  }
  elseif ($element['#required']) {
    $form_state
      ->setError($element, t('You must upload a file.'));
  }
}