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.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

tmountjr created an issue. See original summary.

tmountjr’s picture

Issue summary: View changes
tmountjr’s picture

Issue summary: View changes
jrockowitz’s picture

The 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.

jrockowitz’s picture

Version: 8.x-5.0-beta24 » 8.x-5.x-dev

The ordering on maxlength and minlength inputs also need to be changed to minlength than maxlength in the UI.

  • jrockowitz committed ee1aefe on 2929796-hidden-validation
    Issue #2929796 by jrockowitz: Hidden field validation on min/max length
    

  • jrockowitz committed 7fe7f18 on 2929796-hidden-validation
    Issue #2929796 by jrockowitz: Hidden field validation on min/max length.
    
jrockowitz’s picture

Status: Active » Needs review
FileSize
14.69 KB

  • jrockowitz committed bc29798 on 8.x-5.x
    Issue #2929796 by jrockowitz: Hidden field validation on min/max length
    
jrockowitz’s picture

Status: Needs review » Fixed

I committed the patch. Please download the latest dev release to review.

Status: Fixed » Closed (fixed)

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