Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem
Conditional logic using #states API only provides client side validation. The Webform module needs to support server side validation.
Solution
Provide server side validation for #states API.
Goals
Reuse #states API selectors and nomenclature to create a custom Webform specific server side conditional logic validator and manager
Tasks
- Setup basic required single page validation test form
- Add WebformStatesValidator service
- Write test for getting submission value using selector
- Add required validation for all element variants
Notes
- Must support cross page conditional.
- Cross page conditional logic must track which elements are visible for the current page.
- State validation must be triggered last.
- Conditions MUST be validated using the submission data and not to $form_state->getValue(). This will make it possible to support cross-page conditional logic and conditional handlers.
- Parsing :input selector
- First part of the name array is always to element key
API changes
- WebformElementBase::getElementSelectorInputValue($element, $webform_submission, $selector)
- WebformStatesValidator service
Test forms
- All conditions will require a single element
- All conditions will disable a single element
- All conditions will hide a single element
Element variants
- Basic text value
- Boolean
- Composite
- Other
- Entity reference
- Options
- Likert
- Computed
References
- \Drupal\Core\Condition\ConditionAccessResolverTrait::resolveConditions
Comment | File | Size | Author |
---|---|---|---|
#23 | server_side_conditional-2900719-23.patch | 75.94 KB | jrockowitz |
| |||
#20 | server_side_conditional-2900719-20.patch | 74.69 KB | jrockowitz |
#7 | server_side_conditional-2900719-6.patch | 30.28 KB | jrockowitz |
|
Comments
Comment #2
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedComment #7
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedComment #15
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedComment #20
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedComment #23
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedComment #26
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedComment #27
katannshaw CreditAttribution: katannshaw at Promet Source for Promet Source commented@jrockowitz: I attempted to apply your patch from #23 and received an error stating that the patch cannot be applied. Here's my setup
Changed webform version from 8.x-5.0-beta to 8.x-5.x-dev
Added your patch to composer.json like this:
"patches": {
"drupal/webform": {
"Server side Conditional Logic using #states API": "https://www.drupal.org/files/issues/server_side_conditional-2900719-23.p..."
}
}
Running `composer install --verbose` and `composer update drupal/webform --verbose` returned this result:
Do you have a suggestion on how I can fix this issue?
Kat
Comment #28
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedThe patch has been committed to the latest dev release.
Comment #29
katannshaw CreditAttribution: katannshaw at Promet Source for Promet Source commentedWell that was a brain fart moment... Thanks.
Comment #30
larowlanFYI - this should use the third argument.
https://3v4l.org/oPqTY
Comment #31
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedI was not aware of the third argument. I am surprised that it is not used more frequently in core.
https://www.drupal.org/project/issues/drupal?text=in_array+strict&versio...
I created a ticket to #2903561: Review in_array() usage and add the strict argument as needed.
Comment #33
stiras CreditAttribution: stiras commentedI am not sure if I can ask my question here or should I open a new issue, but since my question is related to that topic, I am posting it here.
I am using server-side conditional logic and everything works fine except for the file (document) upload field - it is marked as required but no validation is performed when no file is actually uploaded. When using required as FORM VALIDATION (not as CONDITIONAL LOGIC required) the validation works but that's not what I need.
Thanks for your help and time!
Comment #34
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedPlease open a new ticket.