You are here

public function SchedulerAdminForm::validateForm in Scheduler 8

Same name and namespace in other branches
  1. 2.x src/Form/SchedulerAdminForm.php \Drupal\scheduler\Form\SchedulerAdminForm::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/SchedulerAdminForm.php, line 104

Class

SchedulerAdminForm
Main administration form for the Scheduler module.

Namespace

Drupal\scheduler\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  $hide_seconds = $form_state
    ->getValue([
    'hide_seconds',
  ]);

  // If date-only is enabled then check if a valid default time was entered.
  // Leading zeros and seconds can be omitted, eg. 6:30 is considered valid.
  if ($form_state
    ->getValue([
    'allow_date_only',
  ])) {
    $default_time = date_parse($form_state
      ->getValue([
      'default_time',
    ]));
    if ($default_time['error_count'] || strlen($form_state
      ->getValue([
      'default_time',
    ])) < 3) {
      $form_state
        ->setErrorByName('default_time', $this
        ->t('The default time should be in the format @format', [
        '@format' => $hide_seconds ? 'HH:MM' : 'HH:MM:SS',
      ]));
    }
    else {

      // Insert any possibly omitted leading zeroes. If hiding the seconds
      // then ignore any entered seconds and save in H:i format.
      $unix_time = mktime($default_time['hour'], $default_time['minute'], $hide_seconds ? 0 : $default_time['second']);
      $form_state
        ->setValue([
        'default_time',
      ], $this->dateFormatter
        ->format($unix_time, 'custom', $hide_seconds ? 'H:i' : 'H:i:s'));
    }
  }
}