You are here

public function ImceProfileForm::validateForm in IMCE 8

Same name and namespace in other branches
  1. 8.2 src/Form/ImceProfileForm.php \Drupal\imce\Form\ImceProfileForm::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

src/Form/ImceProfileForm.php, line 270

Class

ImceProfileForm
Base form for Imce Profile entities.

Namespace

Drupal\imce\Form

Code

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

  // Check folders.
  $folders = [];
  foreach ($form_state
    ->getValue([
    'conf',
    'folders',
  ]) as $i => $folder) {
    $path = trim($folder['path']);

    // Empty path.
    if ($path === '') {
      continue;
    }

    // Validate path.
    if (!Imce::regularPath($path)) {
      return $form_state
        ->setError($form['conf']['folders'][$i]['path'], $this
        ->t('Invalid folder path.'));
    }

    // Remove empty permissions.
    $folder['permissions'] = array_filter($folder['permissions']);
    $folder['path'] = $path;
    $folders[$path] = $folder;
  }

  // No valid folders.
  if (!$folders) {
    return $form_state
      ->setError($form['conf']['folders'][0]['path'], $this
      ->t('You must define a folder.'));
  }
  $form_state
    ->setValue([
    'conf',
    'folders',
  ], array_values($folders));

  // Call plugin validators.
  $this->pluginManagerImce
    ->validateProfileForm($form, $form_state, $this
    ->getEntity());
  return parent::validateForm($form, $form_state);
}