You are here

public function WebformEntityReferenceWidgetTrait::validateOpenClose in Webform 6.x

Same name and namespace in other branches
  1. 8.5 src/Plugin/Field/FieldWidget/WebformEntityReferenceWidgetTrait.php \Drupal\webform\Plugin\Field\FieldWidget\WebformEntityReferenceWidgetTrait::validateOpenClose()

Validate callback to ensure that the open date <= the close date.

Parameters

array $element: An associative array containing the properties and children of the generic form element.

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

array $complete_form: The complete form structure.

See also

\Drupal\webform\WebformEntitySettingsForm::validateForm

\Drupal\datetime_range\Plugin\Field\FieldWidget\DateRangeWidgetBase::validateOpenClose

File

src/Plugin/Field/FieldWidget/WebformEntityReferenceWidgetTrait.php, line 356

Class

WebformEntityReferenceWidgetTrait
Trait for webform entity reference and autocomplete widget.

Namespace

Drupal\webform\Plugin\Field\FieldWidget

Code

public function validateOpenClose(array &$element, FormStateInterface $form_state, array &$complete_form) {
  $status = $element['status']['#value'];
  if ($status === WebformInterface::STATUS_SCHEDULED) {
    $open_date = $element['scheduled']['open']['#value']['object'];
    $close_date = $element['scheduled']['close']['#value']['object'];

    // Require open or close dates.
    if (empty($open_date) && empty($close_date)) {
      $form_state
        ->setError($element['scheduled']['open'], $this
        ->t('Please enter an open or close date'));
      $form_state
        ->setError($element['scheduled']['close'], $this
        ->t('Please enter an open or close date'));
    }

    // Make sure open date is not after close date.
    if ($open_date instanceof DrupalDateTime && $close_date instanceof DrupalDateTime) {
      if ($open_date
        ->getTimestamp() !== $close_date
        ->getTimestamp()) {
        $interval = $open_date
          ->diff($close_date);
        if ($interval->invert === 1) {
          $form_state
            ->setError($element['scheduled']['open'], $this
            ->t('The @title close date cannot be before the open date', [
            '@title' => $element['#title'],
          ]));
        }
      }
    }
  }
}