Why would hidden field trigger data change although not changed?
Why would hidden field trigger data change although not changed?
Editor 2.0.8
I have done a big update to my system, which apparently has a bug and I can't return back. The only option is to find the bug for me.
I have some hidden fields I update on the server side based on some row values. But they trigger data change while inline editing on client side, although data hasn't changed.
Two fields on server-side code:
Field::inst('product_code')->setFormatter(Format::ifEmpty(null)),
Field::inst('product_link')->setFormatter(Format::ifEmpty(null)),
The product_code is filled by the user and server side code updates the hidden field product_link accordingly. Client side code for them:
{ name: "product_code", label: "Product Code" },
{ name: "product_link", type: "hidden" },
Neither of the fields have a visible column on the table, so they are not available for inline editing. They can be edited only through the edit button (main Editor form). The function which updates product_link runs only by "validatedEdit" and "writeCreate" events on server side.
Now if click on a cell to edit it inline and hit enter without changing anything, the editor still submits if the product_link is null in the database. If the product_link is not null in the database, then the editor behaves the way it is expected and closes the form without submitting.
But this doesn't apply to the product_code. Although its formatter is also null, the editor behaves the way it is expected and doesn't submit the form whether the product_code is null in the database or not. The only difference between these fields on client side is one of them being hidden. On the server side, product_link is updated only if the product_code is not null. (Which shouldn't effect the client side, because the form shouldn't submit in the first place.)
What could have I done wrong?
I have modified ajax.data
to send old values to the server like this:
ajax: {
url: env.base,
data: function (d) {
if (editor.modifier() != null) {
console.log(d)
d.old_values = {};
table.rows(editor.modifier()).data().each(function (row) {
d.old_values[row.id] = row;
});
}
}
},
When I compare old_values with row data, I noticed something conspicuous. If the product_code is null in the database, it is also null in the old_values set but it is empty string in the row set. But the product_link is always null on both sets.
Data in old_values:
"product_code": null,
"ifxlk": null,
Data in actual row:
"product_code": "",
"ifxlk": null,
What could be causing this?
Answers
So the problem is almost certainly the null value. Neither HTTP parameters, nor DOM elements, have a null option - it is simply empty string, or a string value. Hence the need for
ifEmpty(null
).By default, Editor will have all fields available to be editable, thus allowing the value of one field to effect another, even if only one is visible. If you want a single field only, you could set the
scope
to becell
- see the docs here.The other option is to use a get formatter at the server-side to change the
null
into an empty string (and then you haveifEmpty()
to convert it back. This works okay, as long as you don't neednull
and empty string as distinct values.Improved
null
handling is something that I have plans for in a future Editor update!Allan
Using a get formatter solves the problem (as I don't need a distinction between null and empty string, at least for now). Then is it about null handling of the Editor?
Yes. When you read a value from a DOM
input
, there is no way to know that a value isnull
since it is always a string. You need some sort of second channel, which is the improvement that I plan to make for Editor at some point in future.Good to hear it is working for you just now.
Allan