Equal matching validation fails if component is nested within a container

Created on 22 January 2024, 11 months ago
Updated 23 January 2024, 11 months ago

Problem/Motivation

When you want to perform an equal match validation and the form components you are comparing the current one to are nested inside a container, it cannot find the components of the form to compare against. The hook function that adds the configuration options to the webform UI element form calls on $webform->getElementsInitializedAndFlattened() to provide the list of other elements you can choose from, so there's no problem selecting the elements you want to compare to. However, in the validation, it just assumes the form is flat and loops through $form['elements'] to see if one with a matching key is present at that level of the form.

Steps to reproduce

In any given webform, nest some elements inside a container of any type. Add an equal validator on any given element in the form to check that it is equal to one of the elements nested inside the container. Attempt to submit the form and see that the validation simply doesn't happen - or you might get an error about some other completely different element not being equal (this is what happened to me).

Screenshot attached showing some dumps during testing of the validation. One interesting thing to note is a field with key "user_preference" that it's looking for that was not even one I could choose to select in the form element config UI. Yet it shows up in the YML exported by config export.

Proposed resolution

Update the code logic in the validator to search the form array for the key regardless of where it is nested.

🐛 Bug report
Status

Closed: cannot reproduce

Version

2.0

Component

Code

Created by

🇨🇦Canada teknocat

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @teknocat
  • 🇨🇦Canada teknocat

    Ok I'm confused now because after reviewing the code and "fixing" it in a couple of different ways, as well as re-saving some form components, it started behaving as expected and the weird issues went away. I saw how the getFormElementAccess() method works if the component is not found at the top level of the elements array and that takes care of the recursive search, so I'm not sure why it didn't seem to be finding the right elements at first and gave an error about a component that wasn't even in the list to be compared for equality.

    Seems it was just something wonky about the webform config on my side, so this issue can likely be closed.

  • Status changed to Closed: cannot reproduce 11 months ago
Production build 0.71.5 2024