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.
Hidden Flexbox layouts with required sub-elements throw errors when they shouldn't (because the required elements are hidden). The following YAML form code reproduces the issue.
checkbox:
'#type': checkbox
'#title': 'Checkbox (leave unchecked and submit the form)'
flexbox:
'#type': flexbox
'#states':
visible:
':input[name="checkbox"]':
checked: true
required_text:
'#type': textfield
'#title': 'Required Text'
'#required': true
text:
'#type': textfield
'#title': Text
Comment | File | Size | Author |
---|---|---|---|
#34 | 2946681-33.patch | 14.09 KB | jrockowitz |
| |||
#34 | interdiff-2946681-22-33.txt | 7.35 KB | jrockowitz |
#22 | 2946681-22.patch | 6.65 KB | jrockowitz |
|
Comments
Comment #2
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedThe issue is that the flex container does not fully support #states and is using a custom #states wrapper.
@see \Drupal\webform\Plugin\WebformElementBase::prepareWrapper
@see \Drupal\webform\Utility\WebformElementHelper::fixStatesWrapper
Comment #3
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedThe attached patch is adding the missing .js-form-wrapper to the flexbox container and remove the states wrapper workaround.
Comment #4
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedThe attached patch is also trying to remove the states wrapper from composite elements.
Comment #6
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedThe attached patch still has a broken test but is a better long-term solution.
Comment #12
nodecode CreditAttribution: nodecode commentedAfter seeing the commit, updated to the latest dev and it hasn't changed anything.
Comment #13
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedThe commits are to a feature branch called '2946681-sub-element-validation'
Comment #14
nodecode CreditAttribution: nodecode commentedGotcha. I'll sit tight until it's ready for testing.
Comment #16
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedI am not sure this going to be the final solution but I want to get this patch tested.
Comment #18
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedComment #20
nodecode CreditAttribution: nodecode commentedOk, I'm ready to test as soon as it passes muster with the Testing module
Comment #22
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedComment #23
nodecode CreditAttribution: nodecode commentedIt's a solid start as it fixes the original example, but the #states do not iterate into the Flexbox's sub-elements if they're dependent upon each other. I was able to break it again with the following simple example:
Comment #24
nodecode CreditAttribution: nodecode commentedComment #25
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedYour example is having the flexbox #states iterate to the radios and then iterate to the text field. I don't think this can be supported. I will look into the problem a little more. The patch does fix the original example.
Comment #26
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedI think have to explicitly code the conditions.
Comment #27
nodecode CreditAttribution: nodecode commentedI guess I'm fundamentally misunderstanding the problem, but if the accepted logic is such that a required element inside a Flexbox should no longer be required when that Flexbox is hidden... is it not logical/possible for ALL sub-elements of that Flexbox to have their "required" property overridden, regardless of additional states logic within the flexbox? It's not even a matter of iteration in this case, just the blanket application of a property setting.
Comment #28
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedThe visible #states for the 'text_field' is taking precedences over parent flexbox element states.
We are not cascading the #states from parent element to a child element, when the child element is defining its own #states.
Comment #30
nodecode CreditAttribution: nodecode commentedI always appreciate your patient explanations. I'm sure this behavior choice was made for good reason that I don't fully grasp at the moment. In this isolated case I don't see the logic, but if that's how Webform works best, then my testing shows the issue is fixed (with the workaround for more complex situations).
My concern is simply that a hidden field is throwing an error message and issues like this may continue to bog down the issue queue in the future as adoption of the module increases.
Thank you!
Comment #31
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedCascading conditionals is really hard to manage and test. I am open to any help improving the Webforms conditional logic. Keep in mind that Webform is leveraging Drupal's #states API which can also be improved.
Comment #32
andypostLooks propagation works fine
better use
!empty()
here, IMOComment #34
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commented@andypost Thanks for the suggestion. The attached patch uses !empty() and removes some duplicate code.
Comment #36
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedComment #37
nodecode CreditAttribution: nodecode commentedJust to be clear for anyone who's wondering in the future. This patch does fix the original issue but does not fix the example in #23 unless you use the workaround in #26.