You are here

public function CalendarEventEmbeddedWidget::validateForm in Opigno calendar event 3.x

Same name in this branch
  1. 3.x src/CalendarEventEmbeddedWidget.php \Drupal\opigno_calendar_event\CalendarEventEmbeddedWidget::validateForm()
  2. 3.x src/Form/CalendarEventEmbeddedWidget.php \Drupal\opigno_calendar_event\Form\CalendarEventEmbeddedWidget::validateForm()
Same name and namespace in other branches
  1. 8 src/CalendarEventEmbeddedWidget.php \Drupal\opigno_calendar_event\CalendarEventEmbeddedWidget::validateForm()

Validation handler.

Parameters

array $form: The form array.

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

File

src/CalendarEventEmbeddedWidget.php, line 196

Class

CalendarEventEmbeddedWidget
Provides a widget to attach a calendar event to an entity.

Namespace

Drupal\opigno_calendar_event

Code

public function validateForm(array $form, FormStateInterface $form_state) {
  $widget_state =& $this
    ->getWidgetState($form, $form_state);
  $has_errors = $form_state
    ->hasAnyErrors();
  $has_nested_errors = FALSE;
  foreach (array_keys($widget_state['items']) as $delta) {
    $item_form_object = $this
      ->getItemFormObject($form, $form_state, $delta);
    $item_form_state = $this
      ->getItemFormState($form, $form_state, $delta);

    /* @var \Drupal\opigno_calendar_event\CalendarEventInterface $calendar_event */
    $calendar_event = $item_form_object
      ->validateForm($form[static::ELEMENT_NAME][$delta], $item_form_state);

    // If the calendar event widget is not populated we can safely ignore the
    // subform validation errors, otherwise we need to proxy them to the
    // parent form state.
    if ($this
      ->isCalendarEventPopulated($calendar_event)) {
      foreach ($item_form_state
        ->getErrors() as $name => $error) {
        $has_nested_errors = TRUE;
        $form_state
          ->setErrorByName($name, $error);
      }
    }
  }

  // Form state tracks errors in a static member so we need to make sure
  // subforms do not affect the main form state if they are not populated.
  if (!$has_errors && !$has_nested_errors) {
    $form_state
      ->clearErrors();
  }
}