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

feat(forms): Add FormBuilder.nonNullable. #45852

Closed
wants to merge 1 commit into from

Conversation

dylhunn
Copy link
Contributor

@dylhunn dylhunn commented May 3, 2022

With typed forms, all FormControls 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.

let nfb = new FormBuilder().nonNullable;
let name = nfb.group({who: 'Alex'}); // FormGroup<{who: FormControl<string>}>
name.reset();
console.log(name); // {who: 'Alex'}

Issue: #13721

@dylhunn dylhunn added action: review The PR is still awaiting reviews from at least one requested reviewer area: forms forms: Controls API Issues related to AbstractControl, FormControl, FormGroup, FormArray. cross-cutting: types labels May 3, 2022
@ngbot ngbot bot modified the milestone: Backlog May 3, 2022
@dylhunn dylhunn modified the milestones: Backlog, v14-candidates May 3, 2022
@dylhunn dylhunn added the target: minor This PR is targeted for the next minor release label May 3, 2022
packages/forms/src/form_builder.ts Outdated Show resolved Hide resolved
packages/forms/src/form_builder.ts Outdated Show resolved Hide resolved
@dylhunn dylhunn added action: cleanup The PR is in need of cleanup, either due to needing a rebase or in response to comments from reviews and removed action: review The PR is still awaiting reviews from at least one requested reviewer labels May 3, 2022
@dylhunn
Copy link
Contributor Author

dylhunn commented May 3, 2022

After discussions with Andrew K and Alex, here are the salient points:

  • nonNull -> nonNullable
  • Provide a field FormBuilder.nonNullable instead of a separate class
  • Implementation should be directly on FormBuilder instead of in a subclass, cleaning up some of the messiness

And then in a follow-up change:

  • new FormControl('foo', {initialValueIsDefault: true}); -> new FormControl('foo', {nonNullable: true});

I'll make these fixes this afternoon.

@dylhunn dylhunn changed the title feat(forms): Add NonNullFormBuilder. feat(forms): Add FormBuilder.nonNullable. May 4, 2022
@dylhunn dylhunn added action: review The PR is still awaiting reviews from at least one requested reviewer and removed action: cleanup The PR is in need of cleanup, either due to needing a rebase or in response to comments from reviews labels May 4, 2022
@dylhunn
Copy link
Contributor Author

dylhunn commented May 4, 2022

This has been updated with the changes above.

@dylhunn
Copy link
Contributor Author

dylhunn commented May 4, 2022

@alxhub Thanks, I've addressed your comments!

@dylhunn dylhunn force-pushed the nonnullbuilder branch 3 times, most recently from 5314e38 to 7df50f0 Compare May 4, 2022 16:59
atscott
atscott previously approved these changes May 4, 2022
Copy link
Contributor

@atscott atscott left a 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

alxhub
alxhub previously approved these changes May 4, 2022
Copy link
Member

@alxhub alxhub left a 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

packages/forms/src/form_builder.ts Outdated Show resolved Hide resolved
}
if (isAbstractControlOptions(validatorOrOpts)) {
// If the second argument is options, then they are copied.
newOptions = validatorOrOpts;
Copy link
Member

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?

AndrewKushnir
AndrewKushnir previously approved these changes May 4, 2022
Copy link
Contributor

@AndrewKushnir AndrewKushnir left a 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

@dylhunn dylhunn added merge: caretaker note Alert the caretaker performing the merge to check the PR for an out of normal action needed or note and removed action: review The PR is still awaiting reviews from at least one requested reviewer labels May 4, 2022
@ngbot ngbot bot added the action: merge The PR is ready for merge by the caretaker label May 4, 2022
@dylhunn
Copy link
Contributor Author

dylhunn commented May 4, 2022

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'}
```
@dylhunn
Copy link
Contributor Author

dylhunn commented May 4, 2022

This PR was merged into the repository by commit 2dbdebc.

@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Jun 10, 2022
@dylhunn dylhunn deleted the nonnullbuilder branch November 30, 2022 20:14
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
action: merge The PR is ready for merge by the caretaker area: forms cross-cutting: types forms: Controls API Issues related to AbstractControl, FormControl, FormGroup, FormArray. forms: strictly typed merge: caretaker note Alert the caretaker performing the merge to check the PR for an out of normal action needed or note target: minor This PR is targeted for the next minor release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants