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): export forms utility functions: isFormArray, isFormGroup… #47718

Closed
wants to merge 1 commit into from

Conversation

fmalcher
Copy link
Contributor

@fmalcher fmalcher commented Oct 10, 2022

This commit exports existing utility functions to check for control instances: isFormControl, isFormGroup, isFormRecord, isFormArray Those are useful when implementing validators that use the specifics of one of those control types. To narrow down the type to what it actually is, we can now use the util functions in validators:

export const myArrayValidator: ValidatorFn = (control) => {
  if (!isFormArray(control)) { return null; }

  // now you can use FormArray-specific members, e.g.:
  if (control.controls.every(c => !!c.value) {
    return { myerror: true }
  } else { return null; }
}

Before, this had to be done manually:

export const myArrayValidator: ValidatorFn = (control) => {
  if (!(control instanceof FormArray)) { return null; }
  // ... 
}

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Documentation content changes
  • angular.io application / infrastructure changes
  • Other... Please describe:

What is the current behavior?

Issue Number: N/A

What is the new behavior?

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

@pullapprove pullapprove bot requested a review from dylhunn October 10, 2022 16:26
@angular-robot angular-robot bot added the feature Issue that requests a new feature label Oct 10, 2022
This commit exports existing utility functions to check for control instances:
isFormControl, isFormGroup, isFormRecord, isFormArray
Those are useful when implementing validators that use the specifics of one of those control types.
To narrow down the type to what it actually is, we can now use the util functions in validators:

```
export const myArrayValidator: ValidatorFn = (control) => {
  if (!isFormArray(control)) { return null; }

  // now you can use FormArray-specific members, e.g.:
  if (control.controls.every(c => !!c.value) {
    return { myerror: true }
  } else { return null; }
}
```
Copy link
Contributor

@dylhunn dylhunn 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: fw-forms, public-api

@dylhunn dylhunn added this to the v15 feature freeze blockers milestone Oct 10, 2022
@ngbot ngbot bot removed this from the v15 feature freeze blockers milestone Oct 10, 2022
@dylhunn dylhunn added area: forms forms: Controls API Issues related to AbstractControl, FormControl, FormGroup, FormArray. labels Oct 10, 2022
@ngbot ngbot bot modified the milestone: Backlog Oct 10, 2022
@dylhunn dylhunn added the target: minor This PR is targeted for the next minor release label Oct 10, 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 the action: presubmit The PR is in need of a google3 presubmit label Oct 10, 2022
@dylhunn dylhunn added action: merge The PR is ready for merge by the caretaker and removed action: presubmit The PR is in need of a google3 presubmit labels Oct 10, 2022
@dylhunn dylhunn self-assigned this Oct 10, 2022
@jessicajaniuk
Copy link
Contributor

This PR was merged into the repository by commit a8569e3.

@fmalcher fmalcher deleted the formsutilityfunctions branch October 10, 2022 20:03
@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 Nov 10, 2022
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 feature Issue that requests a new feature forms: Controls API Issues related to AbstractControl, FormControl, FormGroup, FormArray. 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