Problem/Motivation
#3382510: Introduce a new #config_target Form API property to make it super simple to use validation constraints on simple config forms, and adopt it in several core config forms landed, yay! Unfortunately it introduced a regression 🐛
So I tried adopting it in my contrib module (see #3394172-4: Adopt Drupal core 10.3 config validation infrastructure), and found a bug: the call to FormStateInterface::setErrorByName() is wrong.
Root cause: despite the name of that method, it does not accept a form element name (as in #name), but a variation thereof: implode('][', $element['#parents']) (this is clear if you look at \Drupal\Core\Form\FormState::setError()).
Steps to reproduce
Trigger any validation error, and you'll notice that the form validation error is not associated with any form element, only with the entire form:

Proposed resolution
One-line change:
- $form_element_name = $map["$config_name:$property_path"]->elementName;
+ $form_element_name = implode('][', $map["$config_name:$property_path"]->elementParents);
yields:

Remaining tasks
User interface changes
None.
API changes
None.
Data model changes
None.
Release notes snippet
None.
| Comment | File | Size | Author |
|---|---|---|---|
| Screenshot 2023-11-03 at 11.09.32 AM.png | 171.95 KB | wim leers | |
| Screenshot 2023-11-03 at 11.10.08 AM.png | 190.32 KB | wim leers |
Issue fork drupal-3398974
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
wim leersArguably this needs tests. OTOH, this is such a trivial change/fix that that seems … excessive.
Why wasn't this caught? Doesn't this have test coverage? Well, we were relying on the test coverage that #3364506: Add optional validation constraint support to ConfigFormBase had introduced:
UpdateSettingsFormTest. That explicitly tests this:But … that just happens to be a case where there is no nesting (no
#tree ⇒ TRUE).Comment #4
smustgrave commentedseems the suggestion in the MR is to remove the property. Not sure if that deserves it's own follow up or if it's simple enough here.
Comment #5
alexpottI've merged this into #3398982: ConfigFormBase + validation constraints: support non-1:1 form element-to-config property mapping again as they are extremely related and really need fixing together.
Comment #6
wim leers👍🙏
Comment #7
alexpottWell it turns out the other part of #3398982: ConfigFormBase + validation constraints: support non-1:1 form element-to-config property mapping again is super complex. Let's get this done first.
Comment #9
borisson_I don't fully understand the entire scope of the slack discussion, but this patch is not very complicated and it looks great. I can't run the test only changes on the MR, but the second MR fails where it is expected to fail. +1
Comment #10
alexpottOpened #3399510: Replace calling FormState::setErrorByName() with an empty string with a new method at catch's request
Comment #11
wim leersIt took me a few minutes to understand the
dark magic 🧙
I think we can make it less mysterious 😄
Comment #12
alexpott@Wim Leers nice idea - I agree and applied your suggestion.
Comment #15
wim leersPushed a better solution than what I suggested on the MR 🤓
Comment #16
catchI just committed/pushed this already. Please open a follow-up or maybe we can quick fix it here.
Comment #17
wim leersOkay, on it!
Comment #19
wim leersComment #20
phenaproximaThis change makes sense to me, and I think it makes the error messages a little clearer. That's not a bad thing. No objections.
Comment #21
alexpottCommitted and pushed f9109dc8600 to 11.x and 56aeb82270c to 10.2.x. Thanks!
Thanks @Wim Leers
Comment #24
wim leersThanks!
See y'all in #3398982: ConfigFormBase + validation constraints: support non-1:1 form element-to-config property mapping again 😊