Aurelia changes model values bound to input fields after code resets value to null
This is sort of a follow on to https://github.com/aurelia/framework/issues/288
As mentioned in that story, it was acceptable for Aurelia to change a model value to an empty string '' from null in the case that a user enters a value and then subsequently backs out that value. What I am seeing now though is the following:
- value is initially null
- user types a value into the text input field
- user hits the form reset button which restores the entire model with a clean copy where the value is null
- the value is subsequently changed to an empty string '' by Aurelia
What I do not expect is that if the code resets a value to null, for it to be changed to an empty string '' by the framework. This causes my form dirty-checking to indicate that the form is dirty even after the user clicks 'Reset' to reset the form.
Is this easily fixable? Or should I just work around the issue by setting all nulls in my model to ''?
Hi @NateRedding, I think this (and #288) are in a grey area- not sure if they're true bugs or supported scenarios. I'll attempt to explain what I mean:
The binding system's responsibility is to keep the view and view-model in sync. This is impossible to do when the model contains a value that cannot be applied to the view. I think the problem stems from the fact that input value binding translates null and undefined model values to empty strings when updating the input element. This is done for the sake of usability. If the ValueAttributeObserver didn't do this, the text "null" or "undefined" would be displayed, forcing people to either initialize their properties with empty string or use value converters to get the desired result and removing any ambiguity. The problem with this approach is the behavior you're seeing- we've got a built in conversion from null/undefined to empty string, but no logic to convert empty string back to null/undefined. The rest of the binding system doesn't know/care about this special case, it expects to be able to keep things in sync as usual. This is why I'm suggesting you either initialize your props or use a value converter- your scenario is too nuanced for the built-in behavior that attempts to help people out with the type-coercion the input element does to values applied to it's value attribute.
Let's leave this open so it can be researched further, there may be a tweak we can make to get the behavior you want. It would be a safer bet to use a converter or one of the other solutions we discussed because these are the supported ways of converting a model value to something that can be represented in the view.
@jdanyow Yep, I had a feeling that was going to be your answer. I will work around the problem for now. I do appreciate you leaving this open as a question for further research.
I'd vote for using converter (maybe it could be provided by the framework).
@jdanyow, question inspired by this issue: Would it be possible to programmatically add converters to form elements? I'm pretty confident that each view or custom element could look up all the form elements and add the converter programmatically just like it is done after parsing the binding expression that contains the converter, but is it possible to add global converter (from one place, for example through Aurelia configure function)?
@atsu85 it's definitely possible rewrite a binding's source expression. Here's an example from aurelia-i18n of a BindingBehavior that rewrites the binding expression to include a value converter:
https://github.com/aurelia/i18n/blob/master/src/t.js#L93-L99
It's probably not clear exactly what is going on there but essentially the binding behavior (we'll call it foo) will rewrite expressions like <input value.bind="something & foo"> to be the equivalent of <input value.bind="something | bar & signal">
Rather than putting the expression rewriting logic in a binding behavior (which forces you to use the behavior in your expressions) you could put the expression rewriting logic in a custom attribute that you apply to a form element. In the custom element's created(view) lifecycle callback you'll have access to view.bindings which is an array of all the bindings in the view. Here's some code that uses the view's bindings array- not for expression rewriting but it does analyze the expression, etc.
So all the tools are there, just a matter of tying it all together in a way that makes sense.
As discussed this scenario commonly occurs when restoring model values that include nulls. Note that restoring twice, does restore the nulls, and allowing the model dirty-checking to work as expected.
Current code behaviour This behaviour is caused by html elements not supporting null values, the value is altered to empty-string. Aurelia supports this in its binding processing. When changing a model property from "some text" to null, this triggers:
- Aurelia binding ValueAttributeObserver coerces null newValue to empty-string
- Value detects element.value not equal newValue, so updates the element with newValue and calls notify
- Notify calls element value subscribers with coerced empty-string
- The Binding subscriber compares newValue empty-string with evaluated source expression which is still null ...
- and so updates source model with newValue empty-string.
To fix this I used a custom value convertor to ensure Binding subscribers always see coerced empty-string. Refer #288 NullToEmptyStringValueConverter by @jdanyow. This stops the update source model. When Binding evaluates source (step 4) it find newValue empty-string equals source empty-string, and therefore does not update the source model.