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

  1. Open a form with multiple elements.
  2. Reorder the elements.
  3. Delete one element -> you get the error.
  4. See that the order of the elements is the original order.
  5. 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.

Issue fork webform-3517077

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

tuuuukka created an issue. See original summary.

tuuuukka’s picture

StatusFileSize
new858.25 KB
tuuuukka’s picture

sorabh.v6’s picture

Assigned: Unassigned » sorabh.v6

Starting to work on this issue.

sorabh.v6’s picture

Assigned: sorabh.v6 » Unassigned
Status: Active » Needs review

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

milosr’s picture

Status: Needs review » Reviewed & tested by the community

I tested it and it works well.

Thank you @sorabh.v6

liam morland made their first commit to this issue’s fork.

jrockowitz’s picture

StatusFileSize
new702 bytes

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

jrockowitz’s picture

Status: Reviewed & tested by the community » Needs review

  • jrockowitz committed f6681335 on 6.3.x
    [#3517077] feat: Error and inconcistency when deleting form element...
jrockowitz’s picture

Status: Needs review » Fixed

Now that this issue is closed, please review the contribution record.

As a contributor, attribute any organization helped you, or if you volunteered your own time.

Maintainers, please credit people who helped resolve this issue.

  • jrockowitz committed f6681335 on 6.x
    [#3517077] feat: Error and inconcistency when deleting form element...

Status: Fixed » Closed (fixed)

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

tuuuukka’s picture

Unfortunately the fix introduced here (changing $delta = count($elements); to $delta = count($elements) + 1; in WebformUiEntityElementsForm::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 + 1 does 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 $delta is 5. I drag one element to another position, at which point the first element gets weight -5 (-$delta per Drupal core). Then I delete another element, so count($elements) becomes 3 and $delta 4, 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's order action 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.

tuuuukka’s picture

StatusFileSize
new2.21 KB

Something like this could be the fix. Adds a custom #process callback for weight element that checks the actual submitted value and adds that to #options if it's not there already.

liam morland’s picture

Please open a child ticket and put your patch into a merge request.

tuuuukka’s picture