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
| Comment | File | Size | Author |
|---|---|---|---|
| #10 | edit_form_submission-2858448-10.patch | 5.98 KB | jrockowitz |
| #8 | edit_form_submission-2858448-8.patch | 5.98 KB | jrockowitz |
| #4 | edit_form_submission-2858448-4.patch | 1.4 KB | jrockowitz |
Comments
Comment #2
jrockowitz commentedYou might just need to alter the 'All' elements form and unset #required via custom code.
Comment #3
robpowellWell 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.
Comment #4
jrockowitz commentedOne 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...
If this utility is helpful I can write some unit tests and commit it.
Comment #5
robpowellOh this is cool and an awesome example of how powerful by reference can be. I ended up doing this way:
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.
Comment #6
jrockowitz commentedThanks for writing the tests. They should be simple phpunit tests that verify that the hierarchical elements and flattened elements are insync.
Comment #7
jrockowitz commented@robpowell I need this patch committed for a migration that I am working. Do you have any tests written?
Comment #8
jrockowitz commentedComment #10
jrockowitz commentedComment #12
jrockowitz commentedI committed the patch. Please download the latest release to review.