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.
I have a Contact (Composite element) on the second page of my webform. The element is set to accept unlimited values (everything works fine if it is set to a fixed amount). When I set one of the fields of that element to required I get a validation error on page 1 stating the element on page 2 is required.
Comment | File | Size | Author |
---|---|---|---|
#32 | validation_fails_when-2878478-32.patch | 32.39 KB | jrockowitz |
| |||
#30 | validation_fails_when-2878478-30.patch | 32.38 KB | jrockowitz |
#25 | validation_fails_when-2878478-25.patch | 23.92 KB | jrockowitz |
#19 | validation_fails_when-2878478-19.patch | 18.72 KB | jrockowitz |
#14 | apply.png | 43.33 KB | mukeshMukesh12 |
Comments
Comment #2
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedThe below code duplicates the issue...
Comment #3
sonneworks CreditAttribution: sonneworks commentedI managed to get arround this by overriding validateWebformComposite in the WebformCompositeBase class (I needed my own class extending this one anyway)
note the extra condition
&& $form_state->getUserInput()['op'] != 'Next Page >'
it's not pretty but it works ... it is language dependent though
Comment #4
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commented@sonnework You have right idea. The #access property needs to checked before validating a composite element.
Comment #6
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedI committed the patch. Please download and review the latest dev release.
Comment #8
collin.rickford CreditAttribution: collin.rickford commentedI have run into a similar issue with any custom composite element which includes a datelist. If the datelist is required and the element it belongs to is on any page except the first page, the first page cannot be submitted. An error is displayed:
The %field date is required.
(This is produced by the validation method of the DateList element class) The issue seems to be that somehow the datelist element's validation is being triggered even though it is on a different page - validateWebformComposite can be overridden without affecting the issue at all, so the validation seems to be triggered in some other way.I have attached a small module with a custom composite element and sample webform which demonstrates the issue.
Comment #9
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedThe Datelist::validateDatelist has to check the $element['#access'] property before triggering validation. We should be able to override this validation handler and fix the issue.
Comment #10
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedThe attached webform replicates the issue using the 'webform_test_composite' provided by the webform_test_element.module.
You need to set
$settings['extension_discovery_scan_tests']
to TRUE in local.settings.php to enable the webform_test_element.module.Comment #11
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedSolution/issue/workaround has something to do with \Drupal\webform\Plugin\WebformElement\DateBase::preValidateDate.
Comment #12
collin.rickford CreditAttribution: collin.rickford commentedModifying the workaround to unset the datelist's formstate value if the input is empty seems to work.
Comment #13
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedComment #14
mukeshMukesh12 CreditAttribution: mukeshMukesh12 at gai Technologies Pvt Ltd commentedYes workaround-edit-2878478-12.patch is working fine for custom composite element like datelist.
Comment #19
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedThe attached patch is far from complete but I need to run it thru the test bot to make sure my refactoring has not broken anything...yet.
Comment #25
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedComment #30
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedComment #32
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedComment #35
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedI committed the patch. Please download the latest dev release to review.
Comment #37
larowlanFWIW this broke custom behaviours in our theme where we rendered a required marker for fields that were marked as required.
Because the marking as required only happens in JS now, that twig file doesn't get a chance to intervene.
We solved it by adding a state event listener in JavaScript.
In my opinion, using the #limit_validation_errors feature of Form API to limit errors to page one might have been another approach.
Comment #38
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commented@larowlan The issue with using #limit_validation_errors is that the #element_validate callback for excluded form elements never get executed and occasionally #element_validate is used to alter the submitted value.
@see \Drupal\Core\Render\Element\PasswordConfirm::validatePasswordConfirm
Comment #39
larowlanOk - thanks