Problem/Motivation

While writing custom validators for a huge multi-page form with several layers of nesting where several of the lower components use the same form key(but on different 'branches'), I noticed some of my validators weren't getting executed. Some other validators got executed even though no components on the current page were involved in the rules using them.

This came down to the rules being filtered out because no form keys matched the components on the page. The main issue was that the function getting the form key from the component only returns the key part for the lowest level. Sometimes that matched no components and sometimes it matched other components, depending on which other components using the same last part of the key were present.

Proposed resolution

Filtering out rules by form key got more complicated when comparing the full keys gotten by iterating through all component parents, so I started looking for another way, preferably using the component id. Turns out that works well since the flat list of form values is already indexed by component id, and any components not included on the page are already excluded from it.

Remaining tasks

Review, test.

User interface changes

None

API changes

Two functions removed (not needed anymore): webform_validation_get_field_keys() and webform_validation_rule_get_formkeys().
These functions would not properly handle nested keys and only considered the lowest level.

Data model changes

None

CommentFileSizeAuthor
webform_validation.nested-validation.patch4.35 KBTwoD
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

TwoD created an issue. See original summary.

Liam Morland’s picture

Thanks very much for the patch.

We can't remove functions that do not start underscore since that is an API change (they probably should have been internal functions to begin with). The comments can be changed to indicate that the function is deprecated.

I don't understand the purpose of the changes in the second hunk (-204,21 +187,18).

No need to add && $rules to the if statement. If $rules is empty, it will be an empty array and the code in the foreach will not run anyway.

No need to call count. if (!array_intersect_key(... will work too.

Liam Morland’s picture

Status: Needs review » Needs work

  • Liam Morland committed 610a02b on 7.x-1.x
    Issue #2841817 by TwoD, Liam Morland: Validate nested components
    
Liam Morland’s picture

Assigned: Unassigned » Liam Morland
Status: Needs work » Fixed

I committed code very similar to your first hunk. I put deprecation notices on the functions that are no longer used. If you still feel the second hunk is needed, please open a new issue about that.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.