You are here

public function ImportForm::validateForm in Bibliography & Citation 8

Same name and namespace in other branches
  1. 2.0.x modules/bibcite_import/src/Form/ImportForm.php \Drupal\bibcite_import\Form\ImportForm::validateForm()

Form validation handler.

Parameters

array $form: An associative array containing the structure of the form.

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

Overrides FormBase::validateForm

File

modules/bibcite_import/src/Form/ImportForm.php, line 90

Class

ImportForm
Common import form.

Namespace

Drupal\bibcite_import\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  $all_files = $this
    ->getRequest()->files
    ->get('files', []);
  if (!empty($all_files['file'])) {

    /** @var \Symfony\Component\HttpFoundation\File\UploadedFile $file_upload */
    $file_upload = $all_files['file'];
    if ($file_upload
      ->isValid()) {
      $form_state
        ->setValue('file', $file_upload
        ->getRealPath());
      $format_id = $form_state
        ->getValue('format');
      $format = $this->formatManager
        ->getDefinition($format_id);
      try {
        $data = file_get_contents($form_state
          ->getValue('file'));
        $decoded = $this->serializer
          ->decode($data, $format_id);
        $form_state
          ->setValue('decoded', $decoded);
      } catch (\Exception $exception) {
        $err_string = $this
          ->t('@format file content is not valid.<br>%ex', [
          '@format' => $format['label'],
          '%ex' => $exception
            ->getMessage(),
        ]);
        $form_state
          ->setErrorByName('file', $err_string);
      }
      return;
    }
  }
  else {
    $form_state
      ->setErrorByName('file', $this
      ->t('The file could not be uploaded.'));
  }
  parent::validateForm($form, $form_state);
}