Problem/Motivation

My wizard form skips pages based off of user answers. Think of it as paged based state, if on page 2 user answers no, they skip to page 5; and so on. This works out pretty well because on the page, the form validation is never triggered so I can keep fields required. One other feature to my form, an admin needs to come back and update a field with a value so we can track the record. Via edit/all, we can now edit any field on a wizard form because we load all the fields for the admin user to update/edit. The issue with this approach is now all required fields that may have been skipped are no required.

Proposed resolution

I tried the following approach but am still running into issues. Step 1, because the fields are all loading on one page we can have state logic set the required-ness of the fields. This will make them optional and we will not trigger Html5 validator. Unfortunately, there is still some server side validation that are checking if these fields are set. Using a hook_form_alter, I should be able to override the validation function so these are ignored.

  $url = Url::fromRoute('<current>')->toString();
    $pos = strpos($url, 'edit/all');
    if($pos) {
      $form['#validate'] = '';
    }

However, this does not work. The fields are still being required.

Remaining tasks

User interface changes

API changes

Data model changes

Comments

robpowell created an issue. See original summary.

jrockowitz’s picture

You might just need to alter the 'All' elements form and unset #required via custom code.

robpowell’s picture

Well If I am going to parse over the fields, I might as well only display the one field the admin user can edit. Do we have an example of editing the rendering of the form? I'll try to get this figured out today.

jrockowitz’s picture

Status: Active » Needs review
StatusFileSize
new1.4 KB

One trick would be to flatten the elements into an associative array of element references. The webform requires unique keys for all elements so it is possible to flatten the a webforms render array.

The attached patch provides a helper the allows you to do something like...

$elements =& \Drupal\webform\Utility\WebformFormHelper::flattenElements($form);
$elements['email']['#access'] = FALSE;

If this utility is helpful I can write some unit tests and commit it.

robpowell’s picture

Oh this is cool and an awesome example of how powerful by reference can be. I ended up doing this way:

$elements =& \Drupal\webform\Utility\WebformFormHelper::flattenElements($form);
      $no_form_validation_elements = ['p2_name', 'p3_name', 'p4_name', 'p5_name'];
      foreach ($no_form_validation_elements as $no_validate_element){
        $elements[$no_validate_element]['#access'] = FALSE;
      }
If this utility is helpful I can write some unit tests and commit it.

If you don't mind, i'd like the opportunity to build out some tests. I'll try to refer to the tests that already exists. I should have this done tomorrow evening.

jrockowitz’s picture

Thanks for writing the tests. They should be simple phpunit tests that verify that the hierarchical elements and flattened elements are insync.

jrockowitz’s picture

@robpowell I need this patch committed for a migration that I am working. Do you have any tests written?

jrockowitz’s picture

StatusFileSize
new5.98 KB

Status: Needs review » Needs work

The last submitted patch, 8: edit_form_submission-2858448-8.patch, failed testing.

jrockowitz’s picture

Status: Needs work » Needs review
StatusFileSize
new5.98 KB

  • jrockowitz committed 7a73500 on 8.x-5.x
    Issue #2858448 by jrockowitz: Edit form submission via edit/all posses...
jrockowitz’s picture

Status: Needs review » Fixed

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

Status: Fixed » Closed (fixed)

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