Skip to content

Commit

Permalink
fix(forms): Allow NonNullableFormBuilder to be injected.
Browse files Browse the repository at this point in the history
Based on early feedback, calling `fb.nonNullable.group(...)` continues to be clunky for a form with many such groups. Allowing `NonNullableFormBuilder` to be directly injected enables the following:

```
constructor(private fb: NonNullableFormBuilder) {}
```
  • Loading branch information
dylhunn committed May 5, 2022
1 parent 3e7d299 commit 4c03f3f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
3 changes: 3 additions & 0 deletions goldens/public-api/forms/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,9 @@ export interface NonNullableFormBuilder {
}>;
}

// @public (undocumented)
export const NonNullableFormBuilder: InjectionToken<NonNullableFormBuilder>;

// @public
export class NumberValueAccessor extends BuiltInControlValueAccessor implements ControlValueAccessor {
registerOnChange(fn: (_: number | null) => void): void;
Expand Down
5 changes: 4 additions & 1 deletion packages/forms/src/form_builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/

import {Injectable} from '@angular/core';
import {inject, Injectable, InjectionToken} from '@angular/core';

import {AsyncValidatorFn, ValidatorFn} from './directives/validators';
import {ReactiveFormsModule} from './form_providers';
Expand Down Expand Up @@ -345,6 +345,9 @@ export interface NonNullableFormBuilder {
asyncValidator?: AsyncValidatorFn|AsyncValidatorFn[]|null): FormControl<T>;
}

export const NonNullableFormBuilder = new InjectionToken(
'NonNullableFormBuilder', {providedIn: 'root', factory: () => inject(FormBuilder).nonNullable});

/**
* UntypedFormBuilder is the same as @see FormBuilder, but it provides untyped controls.
*/
Expand Down

0 comments on commit 4c03f3f

Please sign in to comment.