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.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

sonneworks created an issue. See original summary.

jrockowitz’s picture

The below code duplicates the issue...

page_1:
  '#type': wizard_page
  '#title': 'Page 1'
  text_field:
    '#type': textfield
    '#title': 'Text field'
page_2:
  '#type': wizard_page
  '#title': 'Page 2'
  contact:
    '#type': contact
    '#title': Contact
    '#multiple': true
    '#name__required': true
sonneworks’s picture

I managed to get arround this by overriding validateWebformComposite in the WebformCompositeBase class (I needed my own class extending this one anyway)

public static function validateWebformComposite(&$element, FormStateInterface $form_state, &$complete_form) {
    $value = $element['#value'];
    // Validate required composite elements.
    $composite_elements = static::getCompositeElements();
    foreach ($composite_elements as $composite_key => $composite_element) {
      if (!empty($element[$composite_key]['#required']) && $value[$composite_key] == '' && $form_state->getUserInput()['op'] != 'Next Page >') {//do not validate the entire form when just going to the next page
        if (isset($element[$composite_key]['#title'])) {
          $form_state->setError($element[$composite_key], t('@name field is required.', ['@name' => $element[$composite_key]['#title']]));
        }
      }
    }
  }


note the extra condition && $form_state->getUserInput()['op'] != 'Next Page >'

it's not pretty but it works ... it is language dependent though

jrockowitz’s picture

Status: Active » Needs review
FileSize
807 bytes

@sonnework You have right idea. The #access property needs to checked before validating a composite element.

  • jrockowitz committed f0e8903 on 8.x-5.x
    Issue #2878478 by jrockowitz, sonneworks: Validation fails when adding a...
jrockowitz’s picture

Status: Needs review » Fixed

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

Status: Fixed » Closed (fixed)

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

collin.rickford’s picture

FileSize
3.51 KB

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

jrockowitz’s picture

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

jrockowitz’s picture

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

jrockowitz’s picture

Solution/issue/workaround has something to do with \Drupal\webform\Plugin\WebformElement\DateBase::preValidateDate.

collin.rickford’s picture

Modifying the workaround to unset the datelist's formstate value if the input is empty seems to work.

jrockowitz’s picture

Status: Closed (fixed) » Needs review
mukeshMukesh12’s picture

FileSize
34.53 KB
32.73 KB
43.33 KB

Yes workaround-edit-2878478-12.patch is working fine for custom composite element like datelist.

  • jrockowitz committed f0e8903 on 2878478-datelist-validation
    Issue #2878478 by jrockowitz, sonneworks: Validation fails when adding a...

  • jrockowitz committed bac1800 on 2878478-datelist-validation
    Issue #2878478 by jrockowitz, collin.rickford, mukeshMukesh12,...

  • jrockowitz committed b8aeb34 on 2878478-datelist-validation
    Issue #2878478 by jrockowitz, collin.rickford, mukeshMukesh12,...

  • jrockowitz committed 1ba00ce on 2878478-datelist-validation
    Issue #2878478: Validation fails when adding a required field on second...
jrockowitz’s picture

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

Status: Needs review » Needs work

The last submitted patch, 19: validation_fails_when-2878478-19.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

  • jrockowitz committed f883a72 on 2878478-datelist-validation
    Issue #2878478: Validation fails when adding a required field on second...

  • jrockowitz committed 68b30e6 on 2878478-datelist-validation
    Issue #2878478: Validation fails when adding a required field on second...

  • jrockowitz committed 867c857 on 2878478-datelist-validation
    Issue #2878478: Validation fails when adding a required field on second...

  • jrockowitz committed 79b21cc on 2878478-datelist-validation
    Issue #2878478: Validation fails when adding a required field on second...
jrockowitz’s picture

Status: Needs work » Needs review
FileSize
23.92 KB

Status: Needs review » Needs work

The last submitted patch, 25: validation_fails_when-2878478-25.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

  • jrockowitz committed 4e662e7 on 2878478-datelist-validation
    Issue #2878478: Validation fails when adding a required field on second...
  • jrockowitz committed 7d1ea14 on 2878478-datelist-validation
    Issue #2878478: Validation fails when adding a required field on second...

  • jrockowitz committed 77ce835 on 2878478-datelist-validation
    Issue #2878478: Validation fails when adding a required field on second...

  • jrockowitz committed c3c6314 on 2878478-datelist-validation
    Issue #2878478: Validation fails when adding a required field on second...
jrockowitz’s picture

Status: Needs work » Needs review
FileSize
32.38 KB

Status: Needs review » Needs work

The last submitted patch, 30: validation_fails_when-2878478-30.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

jrockowitz’s picture

Status: Needs work » Needs review
FileSize
32.39 KB

  • jrockowitz committed b3e3506 on 2878478-datelist-validation
    Issue #2878478: Validation fails when adding a required field on second...

  • jrockowitz committed cc9c5c9 on 8.x-5.x
    Issue #2878478 by jrockowitz, collin.rickford, mukeshMukesh12,...
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.

larowlan’s picture

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

jrockowitz’s picture

@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

larowlan’s picture

Ok - thanks