Example #1
0
 /**
  * Returns the error message filed against the given form element.
  *
  * Form errors higher up in the form structure override deeper errors as well
  * as errors on the element itself.
  *
  * @return string|null
  *   Either the error message for this element or NULL if there are no errors.
  *
  * @throws \BadMethodCallException
  *   When the element instance was not constructed with a valid form state
  *   object.
  */
 public function getError()
 {
     if (!$this->formState) {
         throw new \BadMethodCallException('The element instance must be constructed with a valid form state object to use this method.');
     }
     return $this->formState->getError($this->array);
 }
 /**
  * @covers ::getError
  */
 public function testGetError()
 {
     $element = ['#foo' => 'bar'];
     $message = 'bar';
     $this->decoratedFormState->getError($element)->willReturn($message)->shouldBeCalled();
     $this->assertSame($message, $this->formStateDecoratorBase->getError($element));
 }
Example #3
0
 /**
  * {@inheritdoc}
  */
 public function getError(array $element)
 {
     return $this->mainFormState->getError($element);
 }
Example #4
0
 /**
  * Validation callback for a datelist element.
  *
  * If the date is valid, the date object created from the user input is set in
  * the form for use by the caller. The work of compiling the user input back
  * into a date object is handled by the value callback, so we can use it here.
  * We also have the raw input available for validation testing.
  *
  * @param array $element
  *   The element being processed.
  * @param \Drupal\Core\Form\FormStateInterface $form_state
  *   The current state of the form.
  * @param array $complete_form
  *   The complete form structure.
  */
 public static function validateDatelist(&$element, FormStateInterface $form_state, &$complete_form)
 {
     $input_exists = FALSE;
     $input = NestedArray::getValue($form_state->getValues(), $element['#parents'], $input_exists);
     if ($input_exists) {
         $all_empty = static::checkEmptyInputs($input, $element['#date_part_order']);
         // If there's empty input and the field is not required, set it to empty.
         if (empty($input['year']) && empty($input['month']) && empty($input['day']) && !$element['#required']) {
             $form_state->setValueForElement($element, NULL);
         } elseif (empty($input['year']) && empty($input['month']) && empty($input['day']) && $element['#required']) {
             $form_state->setError($element, t('The %field date is required.'));
         } elseif (!empty($all_empty)) {
             foreach ($all_empty as $value) {
                 $form_state->setError($element[$value], t('A value must be selected for %part.', array('%part' => $value)));
             }
         } else {
             // If the input is valid, set it.
             $date = $input['object'];
             if ($date instanceof DrupalDateTime && !$date->hasErrors()) {
                 $form_state->setValueForElement($element, $date);
             } elseif ($form_state->getError($element) === NULL) {
                 $form_state->setError($element, t('The %field date is invalid.', array('%field' => !empty($element['#title']) ? $element['#title'] : '')));
             }
         }
     }
 }