You are here

public function DropzoneJsEbWidget::validate in DropzoneJS 8

Same name and namespace in other branches
  1. 8.2 modules/eb_widget/src/Plugin/EntityBrowser/Widget/DropzoneJsEbWidget.php \Drupal\dropzonejs_eb_widget\Plugin\EntityBrowser\Widget\DropzoneJsEbWidget::validate()
1 method overrides DropzoneJsEbWidget::validate()
InlineEntityFormMediaWidget::validate in modules/eb_widget/src/Plugin/EntityBrowser/Widget/InlineEntityFormMediaWidget.php

File

modules/eb_widget/src/Plugin/EntityBrowser/Widget/DropzoneJsEbWidget.php, line 244

Class

DropzoneJsEbWidget
Provides an Entity Browser widget that uploads new files.

Namespace

Drupal\dropzonejs_eb_widget\Plugin\EntityBrowser\Widget

Code

public function validate(array &$form, FormStateInterface $form_state) {
  $trigger = $form_state
    ->getTriggeringElement();

  // Validate if we are in fact uploading a files and all of them have the
  // right extensions. Although DropzoneJS should not even upload those files
  // it's still better not to rely only on client side validation.
  if ($trigger['#type'] == 'submit' && $trigger['#name'] == 'op' || $trigger['#name'] === 'auto_select_handler') {
    $upload_location = $this
      ->getUploadLocation();
    if (!file_prepare_directory($upload_location, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) {
      $form_state
        ->setError($form['widget']['upload'], $this
        ->t('Files could not be uploaded because the destination directory %destination is not configured correctly.', [
        '%destination' => $this
          ->getConfiguration()['settings']['upload_location'],
      ]));
    }
    $files = $this
      ->getFiles($form, $form_state);
    if (in_array(FALSE, $files)) {

      // @todo Output the actual errors from validateFile.
      $form_state
        ->setError($form['widget']['upload'], $this
        ->t('Some files that you are trying to upload did not pass validation. Requirements are: max file %size, allowed extensions are %extensions', [
        '%size' => $this
          ->getConfiguration()['settings']['max_filesize'],
        '%extensions' => $this
          ->getConfiguration()['settings']['extensions'],
      ]));
    }
    if (empty($files)) {
      $form_state
        ->setError($form['widget']['upload'], $this
        ->t('At least one valid file should be uploaded.'));
    }

    // If there weren't any errors set, run the normal validators.
    if (empty($form_state
      ->getErrors())) {
      parent::validate($form, $form_state);
    }
  }
}