-
Notifications
You must be signed in to change notification settings - Fork 24.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Reactive forms cleared input becomes empty string instead of null #45317
Comments
What do you mean by "clear it"? Just removing the data with backspace? Then it makes sense to me that the resulting data stays the empty string.
An option might work but seems out-of-place to me. This use-case would likely fit under #3009, which can be achieved today using a custom value accessor. You should be able to write a directive similar to |
Yes, I mean using the backspace. It seems to me counter intuitive that upon patching the control value is null, and after having been cleared it becomes an empty string. Thanks! |
I see this method in
So shouldn't the value be '' (empty string) since the first patching? I find this behavior inconsistent... |
Yeah, writing the |
The issue seems to come directly from how the
angular/packages/forms/src/directives/shared.ts Lines 337 to 349 in 3a60063
Which means we basically need to "hack" the exiting one, with something like this on the constructor of AppModule for example: DefaultValueAccessor.prototype.registerOnChange = function (fn: (_: string | null) => void): void {
this.onChange = (value: string | null) => {
fn(value === '' ? null : value);
};
}; |
Glad I came across this - In my case, I read the information from my strongly-typed We made a design decision to not use |
@jarodsmk this is exactly what we're experiencing: strongly typed |
You say strongly typed but you don't seem to be using: https://gcanti.github.io/io-ts-types/modules/NonEmptyString.ts.html |
I also find the behavior to be inconsistent. At the very least, I would expect some type of option on the form control to treat empty values as null, particularly when the control is initially patched with a null value. In my particular case, I keep a copy of the initial value around for comparison purposes later. When I call .getRawValue(), it makes the comparisons I have to do w/ the initial value feel pretty hacky and awkward. |
Which @angular/* package(s) are the source of the bug?
forms
Is this a regression?
No
Description
I have created a minimal reproducible sample of the problem here: https://stackblitz.com/edit/angular-ivy-4ltwow
Here you can see the mocked model input (that I receive from server API in the real app) is null at first, but after I edit the value in textbox and clear it, it becomes empty string.
I test this value server side to see if it has changed since before, and empty string is different from null so I have to test the difference from null AND empty string literally everywhere.
I have no alternative other than creating a directive and applying on every text input, but there are literally thousands and if I forget to put only one the problem arises again.
Shouldn't
getRawValue
return null if the input control is blanked? Wouldn't it be possibile to include an option in the form initialization (or a parameter ofgetRawValue
) to get this desired behavior?Thanks!
Please provide a link to a minimal reproduction of the bug
https://stackblitz.com/edit/angular-ivy-4ltwow
Please provide the exception or error you saw
No response
Please provide the environment you discovered this bug in (run
ng version
)Anything else?
No response
The text was updated successfully, but these errors were encountered: