You are here

public function ConfigPermListForm::validateForm in Custom Permissions 8

Same name and namespace in other branches
  1. 8.2 src/Form/ConfigPermListForm.php \Drupal\config_perms\Form\ConfigPermListForm::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/ConfigPermListForm.php, line 151

Class

ConfigPermListForm
Class ConfigPermListForm.

Namespace

Drupal\config_perms\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  $values = $form_state
    ->getValues();
  $perms = CustomPermsEntity::loadMultiple();
  foreach ($values['local'] as $key => $perm) {
    if (empty($perm['name']) && empty($perm['path']) && $key != 'new') {
      $entity = CustomPermsEntity::load($perm['id']);
      $entity
        ->delete();
    }
    else {
      if (empty($perm['name'])) {
        $form_state
          ->setErrorByName("local][" . $key . "", $this
          ->t("The name cannot be empty."));
      }
      if (empty($perm['path'])) {
        $form_state
          ->setErrorByName("local][" . $key . "", $this
          ->t("The path cannot be empty."));
      }
      if (array_key_exists($this
        ->configPermsGenerateMachineName($perm['name']), $perms) && !isset($perm['id'])) {
        $form_state
          ->setErrorByName("local][" . $key . "", $this
          ->t("A permission with that name already exists."));
      }
      if (!empty($perm['path'])) {
        $paths = $this
          ->configPermsParsePath($perm['path']);
        foreach ($paths as $path) {
          $url_object = \Drupal::service('path.validator')
            ->getUrlIfValid($path);
          if (!$url_object) {
            $form_state
              ->setErrorByName("local][" . $key . "", $this
              ->t("The path @path is invalid.", [
              '@path' => $path,
            ]));
          }
        }
      }
    }
  }
}