You are here

public function ComponentFormBase::validateForm in Layout Paragraphs 2.0.x

Validate the component form.

Parameters

array $form: The form array.

\Drupal\Core\Form\FormStateInterface $form_state: The form state object.

Overrides FormBase::validateForm

File

src/Form/ComponentFormBase.php, line 236

Class

ComponentFormBase
Class LayoutParagraphsComponentFormBase.

Namespace

Drupal\layout_paragraphs\Form

Code

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

  // First validate paragraph behavior forms.
  foreach ($this
    ->getEnabledBehaviorPlugins() as $behavior_id => $behavior_plugin) {
    if (!empty($form['behavior_plugins'][$behavior_id])) {
      $subform_state = SubformState::createForSubform($form['behavior_plugins'][$behavior_id], $form_state
        ->getCompleteForm(), $form_state);
      $behavior_plugin
        ->validateBehaviorForm($this->paragraph, $form['behavior_plugins'][$behavior_id], $subform_state);
    }
  }

  // Validate the paragraph with submitted form values.
  $paragraph = $this
    ->buildParagraphComponent($form, $form_state);
  $violations = $paragraph
    ->validate();

  // Remove violations of inaccessible fields.
  $violations
    ->filterByFieldAccess($this
    ->currentUser());

  // The paragraph component was validated.
  $paragraph
    ->setValidationRequired(FALSE);

  // Flag entity level violations.
  foreach ($violations
    ->getEntityViolations() as $violation) {

    /** @var \Symfony\Component\Validator\ConstraintViolationInterface $violation */
    $form_state
      ->setErrorByName('', $violation
      ->getMessage());
  }
  $form['#display']
    ->flagWidgetsErrorsFromViolations($violations, $form, $form_state);
}