You are here

public static function HMS::validateHMS in HMS Field 8

Form element validation handler for #type 'hms'.

Note that #required is validated by _form_validate() already.

File

src/Element/HMS.php, line 58
Contains \Drupal\hms_field\Element\Hmsfield.

Class

HMS
Provides a one-line text field form element.

Namespace

Drupal\hms_field\Element

Code

public static function validateHMS(&$element, FormStateInterface $form_state, &$complete_form) {
  $value = trim($element['#value']);
  $form_state
    ->setValueForElement($element, $value);
  if ($value !== '' && !\Drupal::service('hms_field.hms')
    ->isValid($value, $element['#format'], $element, $form_state)) {
    $form_state
      ->setError($element, t('Please enter a correct hms value in format %format.', array(
      '%format' => $element['#format'],
    )));
  }
  else {

    // Format given value to seconds if input is valid.
    $seconds = \Drupal::service('hms_field.hms')
      ->formatted_to_seconds($value, $element['#format'], $element, $form_state);
    $form_state
      ->setValueForElement($element, $seconds);
  }
}