Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Angular 14 how to add nonNullable to Form Control which has validations

Tags:

angular14

form: FormGroup;

  createMyForm() {
    this.form = new FormGroup<any>({
      firstName: new FormControl<any>('', [Validators.required]),
      lastName: new FormControl<any>('', { nonNullable: true }),
    });
  }

How to add { nonNullable: true } to firstName

I am trying to add {nonNullable: true } to the form control.

lastName: new FormControl('', { nonNullable: true }),

I am able to add {nonNullable: true } to the control which doesnt have validations, but for the form control which has validations I am not able to add {nonNullable: true }.

firstName: new FormControl('', [Validators.required]),

like image 281
gbr Avatar asked Sep 12 '25 19:09

gbr


1 Answers

createMyForm() {
    this.form = new FormGroup<any>({
      firstName: new FormControl<any>('', {
        nonNullable: true,
        validators: [Validators.required]
      }),
      lastName: new FormControl<any>('', { nonNullable: true })
    });
  }

if all fields has nonNullable:true and you are comfortable injecting form-builder, you can use NonNullableFormBuilder

like image 92
Amit Kumar Avatar answered Sep 16 '25 08:09

Amit Kumar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!