Skip to content
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

Can't set form control to null using setValue update function #116

Closed
chrissena opened this issue Oct 1, 2018 · 5 comments
Closed

Can't set form control to null using setValue update function #116

chrissena opened this issue Oct 1, 2018 · 5 comments
Labels

Comments

@chrissena
Copy link

version 3.0.3

Thanks for creating such a great library!

I am having trouble setting the value of a formControl to null.

According to the docs:

... Controls directly support values of type string, number, boolean, null, and undefined. For object and array values you have to use value boxing.

however when attempting to set the value of a control to null using the setValue update function i.e. setValue(null) the following error occurs.

TypeError: Cannot read property 'hasOwnProperty' of null  
    at isFormState (forms.es5.js:463)
    at setValue (forms.es5.js:2924)

when looking through the code it seems that this caused by

export function isFormState<TValue = any>(state: any): state is FormState<TValue> {
    return state.hasOwnProperty('id') && state.hasOwnProperty('value') && state.hasOwnProperty('errors');
}

should there not be a null check on state or is there a better way of setting the value to null that I am unaware of?

@MrWolfZ
Copy link
Owner

MrWolfZ commented Oct 1, 2018 via email

@chrissena
Copy link
Author

Yes, I can use the uncurried version.

As I am using setValue as part of an updateGroup function I suppose i could write it like
(state => setValue(state, v))

This would just be me currying the function myself (which is the workaround I am currently using) to avoid the error being thrown in the isFormState function. (isFormState would now only be called with the actual state.)

...since the state itself should never be null, just its value may be null.

I thought the whole point of the isFormState function was to check whether the first parameter in setValue is a value or formState (because it is overloaded). i.e. to determine whether the function is being called as curried or uncurried. This means whilst checking there is still a possibility that it could be null (a valid formValue but invalid formState)

This is why I suggested checking for null like...

export function isFormState<TValue = any>(stateOrValue: any): stateOrValue is FormState<TValue> {
    return stateOrValue && stateOrValue.hasOwnProperty('id') && stateOrValue.hasOwnProperty('value') && stateOrValue.hasOwnProperty('errors');
}

@MrWolfZ
Copy link
Owner

MrWolfZ commented Oct 1, 2018 via email

@chrissena
Copy link
Author

no worries, easily done. Thanks for the prompt reply!

@MrWolfZ MrWolfZ added the bug label Oct 1, 2018
@MrWolfZ
Copy link
Owner

MrWolfZ commented Oct 1, 2018

I've just released version 3.0.4 which contains a fix for this bug.

@MrWolfZ MrWolfZ closed this as completed Oct 1, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants