You are here

public function ConfigPermListForm::validateForm in Custom Permissions 8.2

Same name and namespace in other branches
  1. 8 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 195

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['route']) && $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['route'])) {
        $form_state
          ->setErrorByName("local][" . $key . "", $this
          ->t("The route 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['route'])) {
        $routes = config_perms_parse_path($perm['route']);
        foreach ($routes as $route) {
          if (count($this->routerProvider
            ->getRoutesByNames([
            $route,
          ])) < 1) {
            $form_state
              ->setErrorByName("local][" . $key . "", $this
              ->t("The route @route is invalid.", [
              '@route' => $perm['route'],
            ]));
          }
        }
      }
    }
  }
}