You are here

public function JsonapiResourceConfigForm::validateForm in JSON:API Extras 8.2

Same name and namespace in other branches
  1. 8.3 src/Form/JsonapiResourceConfigForm.php \Drupal\jsonapi_extras\Form\JsonapiResourceConfigForm::validateForm()
  2. 8 src/Form/JsonapiResourceConfigForm.php \Drupal\jsonapi_extras\Form\JsonapiResourceConfigForm::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/JsonapiResourceConfigForm.php, line 173

Class

JsonapiResourceConfigForm
Base form for jsonapi_resource_config.

Namespace

Drupal\jsonapi_extras\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  if (!method_exists($this->typedConfigManager, 'createFromNameAndData')) {

    // Versions of Drupal before 8.4 have poor support for constraints. In
    // those scenarios we don't validate the form submission.
    return;
  }
  $typed_config = $this->typedConfigManager
    ->createFromNameAndData($this->entity
    ->id(), $this->entity
    ->toArray());
  $constraints = $typed_config
    ->validate();

  /** @var \Symfony\Component\Validator\ConstraintViolation $violation */
  foreach ($constraints as $violation) {
    $form_path = str_replace('.', '][', $violation
      ->getPropertyPath());
    $form_state
      ->setErrorByName($form_path, $violation
      ->getMessage());
  }
}