function FileExampleReadWriteForm::validateForm

Overrides FormBase::validateForm

File

modules/file_example/src/Form/FileExampleReadWriteForm.php, line 179

Class

FileExampleReadWriteForm
File test form class.

Namespace

Drupal\file_example\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  $destination = $form_state->getValue('destination');
  if (!$destination) {
    $form_state->setError($form['write_file']['destination'], $this->t('You must enter a destination.'));
    return;
  }
  $filename = $this->fileSystem
    ->basename($destination);
  if (!$filename) {
    $form_state->setError($form['write_file']['destination'], $this->t('The destination %destination is not valid.', [
      '%destination' => $destination,
    ]));
    return;
  }
  // For security reasons, we only allow writing to the .txt file.
  if (!str_ends_with($destination, '.txt')) {
    $form_state->setError($form['write_file']['destination'], $this->t("The .txt file is only permitted for the purpose of ensuring the security of the example."));
  }
}