The attached sample form has two basic 'Select' fields, and saving a submission as a draft is enabled.
The fields have conditional logic to enforce one answer only: if 'Select first' is filled, 'Select second' is disabled, and vice-versa.
To reproduce:
- Choose 'a' in First select. (Second select will be immediately disabled)
- Click save. (Form will reload, partially - and correctly - filled)
- Change First select back to '- None -'. (Select second will become available again.)
- Change Second select to z. (First select will be disabled. All normal so far.)
- Click save.
Expected:
On page reload, form should be as it was before clicking save: First Select should is empty and disabled, Select second = z
Observed:
On page reload, First select = 'a' (the value we deleted), Select second = z and now, due to rules, user cannot change either value.
Note:
- This can be done the other way around (start by setting Second select).
- It happens whether AJAX is on or off.
- it *only* happens with '#states' set - with no conditional logic, as you'd expect you can set and unset fields and save drafts without issue.
Webform 5.0.0-rc18
Drupal 8.5.6
All browsers (tested Safari, Chrome, Firefox)
| Comment | File | Size | Author |
|---|---|---|---|
| #11 | 2995154-11.patch | 2.8 KB | jrockowitz |
| #9 | 2995154-8.patch | 8.61 KB | jrockowitz |
| #9 | Screen Shot 2018-08-28 at 5.53.40 AM.png | 61.47 KB | jrockowitz |
| #5 | Edit textfield element.png | 69.71 KB | jrockowitz |
| #5 | 2995154-5.patch | 948 bytes | jrockowitz |
Comments
Comment #2
wturrell commentedI did also try it with checkboxes (checkbox foo disabled if checkbox bar is checked) - all works correctly.
Comment #3
jrockowitz commentedSomething strange is happening because disabled elements should not be posted to the server.
https://www.w3schools.com/tags/att_input_disabled.asp
Attached is webform that includes basic conditional logic to disable a text field and a simple text field that is disabled. Both elements are posting values to the server.
Comment #4
jrockowitz commentedSo Drupal core Form API's default behavior is to set the form state value for any element with a #value.
@see \Drupal\Core\Form\FormBuilder::handleInputElement
I am not sure if there is anything that we can do.
Clearing a disabled element's value does not seem like the right approach. Technically, 'disabled' means the user's entered value is not posted to the server and it makes sense that Form API would use the element #default_value.
Maybe removing disabled/enabled states makes the most sense.
Comment #5
jrockowitz commentedThe attached patch removes support for the enabled/disabled states and displays the below message for webform elements that are using these states.
We could try to dynamically restore these states if they are being used.
Comment #6
wturrell commentedOK understood (I think). I suspect quite a few users might find that error confusing on it's own. I realise why it appears where it does, as the state no longer exists, but…
Just wondering how many other people might already be using that setting in production and will need to redesign forms (which admittedly may not have been working properly anyway). Another odd, possible, use-case: disabling the submit button until a confirmation checkbox is ticked?
I have several workarounds for my situation:
- redesigning form to merge the items from both (or more) select fields into a single dropdown (given only one is required)
- preserving existing layout, but adding a markup element with an error message (wrapped in the standard Drupal messages CSS classes) with its own conditional logic - so it's only shown when an invalid combination of inputs are selected, before user submits form
- using HOOK_webform_submission_form_alter to add a custom validation function
Comment #7
jrockowitz commentedMaybe we just need to document that "Disabled elements do not post data back to the server and the element's server-side default or current value will be preserved and saved to the database."
Comment #8
wturrell commentedYes, I think that would work.
Comment #9
jrockowitz commentedThe attached patch displays a warning message.
I also had to fix a weird but related somewhat issue where the disabled cardinality elements stopped triggering #states.
Comment #10
jrockowitz commentedComment #11
jrockowitz commentedThe attached patch just displays the message from #9. I think this is a reasonable solution. I agree that being able to conditionally disable elements and buttons is a useful feature.
Comment #12
wturrell commentedCan confirm patch applies cleanly and works as expected.
Comment #14
jrockowitz commented@wturrell Thanks for the help.