You are here

protected function RestUIForm::validateFormValuesForMethodGranularity in REST UI 8

Form validation handler when the selected granularity is 'method'.

Parameters

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

See also

\Drupal\restui\Form\RestUIForm::validateForm()

1 call to RestUIForm::validateFormValuesForMethodGranularity()
RestUIForm::validateForm in src/Form/RestUIForm.php

File

src/Form/RestUIForm.php, line 368

Class

RestUIForm
Provides a REST resource configuration form.

Namespace

Drupal\restui\Form

Code

protected function validateFormValuesForMethodGranularity(FormStateInterface $form_state) {

  // At least one method must be checked.
  $method_checked = FALSE;
  $methods = $form_state
    ->getValue([
    'wrapper',
    'methods',
  ]);
  if (!empty($methods)) {
    foreach ($methods as $method => $values) {
      if ($values[$method]) {
        $method_checked = TRUE;

        // At least one format and authentication provider must be selected.
        $formats = array_filter($values['settings']['formats']);
        if (empty($formats)) {
          $form_state
            ->setErrorByName('methods][' . $method . '][settings][formats', $this
            ->t('At least one format must be selected for method @method.', [
            '@method' => $method,
          ]));
        }
        $auth = array_filter($values['settings']['auth']);
        if (empty($auth)) {
          $form_state
            ->setErrorByName('methods][' . $method . '][settings][auth', $this
            ->t('At least one authentication provider must be selected for method @method.', [
            '@method' => $method,
          ]));
        }
      }
    }
  }
  if (!$method_checked) {
    $form_state
      ->setErrorByName('methods', $this
      ->t('At least one HTTP method must be selected'));
  }
}