Berlin, Germany
Account created on 20 March 2009, over 15 years ago
#

Recent comments

🇩🇪Germany tobiberlin Berlin, Germany

tobiberlin created an issue.

🇩🇪Germany tobiberlin Berlin, Germany

I just solved it now the following way for my custom filter plugin:

  public function validateExposed(&$form, FormStateInterface $form_state) {
    if (empty($this->options['exposed'])) {
      return;
    }
    $value = &$form_state->getValue($this->options['expose']['identifier']);
    if (!empty($this->options['expose']['use_operator']) && !empty($this->options['expose']['operator_id'])) {
      $operator = &$form_state->getValue($this->options['expose']['operator_id']);
    }
    else {
      $operator = $this->operator;
    }

    $this->validateExposedValidTime($this->options['expose']['identifier'], $form_state, $operator, $value);
  }

Changes: no stop of validation just because the field is not required, Replaced validateValidTime method call to call my custom validation method.

The method looks like this. The first parameter in this case is a string and instead of calling $form_state->setError() in case of a fals value I use $form_state->setErrorByName():

  protected function validateExposedValidTime(string $elementIdentifier, FormStateInterface $form_state, string $operator, array $value) {
    $operators = $this->operators();

    if ($operators[$operator]['values'] == 1) {
      $convert = strtotime($value['value']);
      if (!empty($elementIdentifier['value']) && ($convert == -1 || $convert === FALSE)) {
        $form_state->setErrorByName($elementIdentifier, $this->t('Invalid date format.'));
      }
    }
    elseif ($operators[$operator]['values'] == 2) {
      if (!empty($value['min'])) {
        $min = strtotime($value['min']);
        if ($min == -1 || $min === FALSE) {
          $form_state->setErrorByName($elementIdentifier, $this->t('Invalid date format.'));
        }
      }
      if (!empty($value['max'])) {
        $max = strtotime($value['max']);
        if ($max == -1 || $max === FALSE) {
          $form_state->setErrorByName($elementIdentifier, $this->t('Invalid date format.'));
        }
      }
    }
  }

As I am not sure what these changes may cause for other views filter plugin extending the Date class, for the options form and in the many other use cases I just adjusted it for our custom plugin as I can assure that it works how it should... but maybe this may help

🇩🇪Germany tobiberlin Berlin, Germany

It seems to me that Date->validateValidTime() is only fitting for calls coming from validateOptionsForm() - and it was re-used for validateExposed() but without realizing that $this->options['expose']['identifier'] has another structure then. For me it's not clear what is going on here - but I think that validateValidTime() will never really work when a validation is done for exposed filter input - which apparently is only done when the field is required.

🇩🇪Germany tobiberlin Berlin, Germany

I can confirm that the patch solves this issue

🇩🇪Germany tobiberlin Berlin, Germany

I had the same error on my page after updating to PHP 8.1 and Drupal 9.5.7 when I submitted a webform. the patch in #11 solved the problem

Production build 0.71.5 2024