-
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
feat(forms): Add FormBuilder.nonNullable
.
#45852
Conversation
After discussions with Andrew K and Alex, here are the salient points:
And then in a follow-up change:
I'll make these fixes this afternoon. |
NonNullFormBuilder
.FormBuilder.nonNullable
.
This has been updated with the changes above. |
@alxhub Thanks, I've addressed your comments! |
5314e38
to
7df50f0
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
reviewed-for: public-api
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewed-for: public-api
} | ||
if (isAbstractControlOptions(validatorOrOpts)) { | ||
// If the second argument is options, then they are copied. | ||
newOptions = validatorOrOpts; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we perhaps assert here that initialValueIsDefault
is not explicitly set to false
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewed-for: public-api
merge-assistance: a small nit was fixed, approvals above. |
With typed forms, all `FormControl`s are nullable by default, because they can be reset to `null`. This behavior is possible to change by passing the option `initialValueIsDefault: true`. However, in a large form, this is extremely cumbersome, as the option must be repeated over and over. Additionally, it is not possible to take full advantage of `FormBuilder`, since `FormBuilder.group` and `FormBuilder.array` will produce nullable controls. This PR introduces a new accessor `FormBuilder.nonNullable`, which produces *non-nullable* controls. Specifically, any call to `.control` will produce controls with `{initialValueIsDefault: true}`, and calls to `.array` or `.group` that implicitly build inner controls will have the same effect. ```ts let nfb = new FormBuilder().nonNullable; let name = nfb.group({who: 'Alex'}); // FormGroup<{who: FormControl<string>}> name.reset(); console.log(name); // {who: 'Alex'} ```
e8ef016
This PR was merged into the repository by commit 2dbdebc. |
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
With typed forms, all
FormControl
s are nullable by default, because they can be reset tonull
. This behavior is possible to change by passing the optioninitialValueIsDefault: true
. However, in a large form, this is extremely cumbersome, as the option must be repeated over and over. Additionally, it is not possible to take full advantage ofFormBuilder
, sinceFormBuilder.group
andFormBuilder.array
will produce nullable controls.This PR introduces a new accessor
FormBuilder.nonNullable
, which produces non-nullable controls. Specifically, any call to.control
will produce controls with{initialValueIsDefault: true}
, and calls to.array
or.group
that implicitly build inner controls will have the same effect.Issue: #13721