Problem/Motivation
When you reorder form elements and delete one element, you get an error:
"The submitted value -XX in the Weight for XXX element is not allowed."
Also the order of the elements seems to be returned to its original state after the deletion, but after saving the form, the elements are once again in the reordered state.
See attached video for an example.
Steps to reproduce
- Open a form with multiple elements.
- Reorder the elements.
- Delete one element -> you get the error.
- See that the order of the elements is the original order.
- Save the form -> the order is now the reorded one.
Proposed resolution
There should be no error when deleting an element after reorder. The order of the elements should be consistent between these steps.
Comments
Comment #2
tuuuukka commentedComment #3
tuuuukka commentedComment #4
sorabh.v6Starting to work on this issue.
Comment #6
sorabh.v6I have created the MR. I worked on it for so much time. Initially, I thought I could just add the validation and then I see that element validations are different than form validation itself and event that didn't worked because there was not identifier to know if the element weight is changed.
The better and I think only approach to solve this was doing some checking on the frontend side using the JS.
It was tricky one. :)
I am adding the warning message when an element is dragged to save the form before making any changes. Even if user ignores that warning and just click on delete action link for the element then I disabled the delete button and show the same message as error in the dialog modal.
Comment #7
milosr commentedI tested it and it works well.
Thank you @sorabh.v6
Comment #9
jrockowitz commented@sorabh.v6 I appreciated your work on the JavaScript approach.
I am very hesitant to add all this additional JavaScript when we want to suppress the unexpected and confusing "The submitted value -XX in the Weight for XXX element is not allowed.".
At the same time, if re-orders, doesn't click save, and deletes and element, it is reasonable to that re-ordering is lost. This is how is works with menus and taxonomies.
Attached is patch that addresses the unexpected error.
Comment #10
jrockowitz commentedComment #13
jrockowitz commentedComment #17
tuuuukka commentedUnfortunately the fix introduced here (changing
$delta = count($elements);to$delta = count($elements) + 1;inWebformUiEntityElementsForm::buildForm()) doesn't seem to actually fix the issue nor the weird error. If the reordering not getting saved is expected behaviour, we could still take a closer look at the error.So, reordering by drag-and-drop and deleting an element still produces this:
The submitted value [X] in the Weight for [Some Element] element is not allowed.I think
+ 1does not fix the issue nor that error because it's applied both before and after the deletion, right? The gap between the two deltas stays the same.So lets say I have four elements in a form. The
count($elements)is 4 which means$deltais 5. I drag one element to another position, at which point the first element gets weight -5 (-$deltaper Drupal core). Then I delete another element, socount($elements)becomes 3 and$delta4, but the weight of the first element is still -5, which is out of the allowed range that now starts from -4 upwards.core/misc/tabledrag.js'sorderaction does not only change the weight of the dragged element, but re-serializes weights for every sibling element too, starting from the most negative value. That's why the first element in this example gets the -5 weight and error is shown.Comment #18
tuuuukka commentedSomething like this could be the fix. Adds a custom
#processcallback for weight element that checks the actual submitted value and adds that to#optionsif it's not there already.Comment #19
liam morlandPlease open a child ticket and put your patch into a merge request.
Comment #20
tuuuukka commentedDone, continuing there: https://www.drupal.org/project/webform/issues/3612678.