Issue Bug Closed

#96424Resetting form validators does not work as expected

Elias Häußler 12/21/21, 17:53 updated 10/04/22, 14:14

Description

Problem

If form validators are to be reset by calling \TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable::setOptions() with second parameter $resetValidators = false, it seems that not all validators are actually reset if there is more than one validator defined. This is because the underlying SplObjectStorage does not operate as someone might expect in combination with foreach.

Steps to reproduce

Generate a basic form with an element and a variant such as follows:

- identifier: element-1
  type: Text
  # ...

- identifier: element-2
  type: Text
  # ...
  validators:
    - identifier: NotEmpty
    - identifier: EmailAddress
  variants:
    -
      identifier: variant-1
      condition: 'formValues["element-1"] == "some-value"'
      validators: []

Now in the Frontend, set the value of element 1 to some-value and submit the form. You should receive a validation error for element 2, even though all validators should be removed in this case.

Solution

A simple solution would be to clone the validators when iterating and performing detaches (as suggested here).

For this, compare the result of the underlying code:

Reviews 4