First off, I realize that the scenario I'm about to describe isn't technically HTML5 spec-approved, and it may be that this is actually a problem with the core Form API. I've discovered that a conditionally-hidden number input that has minlength/maxlength values specified still validates the input length despite being hidden. Take these form elements:
show:
'#type': radios
'#title': 'Show the field?'
'#options': yes_no
'#required': true
year:
'#type': number
'#title': year
'#states':
visible:
':input[name="show"]':
value: 'Yes'
'#min': '1900'
'#max': '2100'
'#step': '1'
If the "year" field is hidden via the condition on the "show" radios, the validation doesn't run on "year", as you would expect. But, if you modify the form like so:
show:
'#type': radios
'#title': 'Show the field?'
'#options': yes_no
'#required': true
year:
'#type': number
'#title': year
'#maxlength': '4'
'#minlength': '4'
'#states':
visible:
':input[name="show"]':
value: 'Yes'
'#min': '1900'
'#max': '2100'
'#step': '1'
...and when you submit the form with "No" selected in the "show" radio, you get a validation error: "year cannot be less than 4 characters but is currently 0 characters long." I assume this module simply passes a form off to the Form API for validation, and if that's the case then this is probably a bug with the Form API. But I think it's partially a problem with the webform module too, since this module seems to remove validation on hidden elements.
Comment | File | Size | Author |
---|---|---|---|
#8 | 2929796-7.patch | 14.69 KB | jrockowitz |
| |||
#4 | webform.webform.issue_2929796.yml | 3.14 KB | jrockowitz |
Comments
Comment #2
tmountjr CreditAttribution: tmountjr commentedComment #3
tmountjr CreditAttribution: tmountjr commentedComment #4
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedThe attached webform replicates the issue.
#maxlength validation is handled by \Drupal\Core\Form\FormValidator::performRequiredValidation
#minlength validation is handler by \Drupal\webform\Plugin\WebformElementBase::validateMinlength
So both Core and Webform are the problem.
We would have to implement a custom #maxlength validation callback that checks if an element is visible before triggering validation. This is some very tricky stuff to fix but it is fixable.
Comment #5
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedThe ordering on maxlength and minlength inputs also need to be changed to minlength than maxlength in the UI.
Comment #8
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedComment #10
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedI committed the patch. Please download the latest dev release to review.