s-f
s-f

Reputation: 2141

Why it's still possible to assign null for non-nullable FormControl?

This simple code returns null. I had the expectation that nonNullable form would have some checks inside. Do you have any idea why it's implemented like this?

I tested it with Angular 18

test = new FormControl('', { nonNullable: true });
// ...
const val: any = null;
this.test.setValue(val);
console.log(this.test.value); // returns null

Upvotes: 0

Views: 37

Answers (1)

Matthieu Riegler
Matthieu Riegler

Reputation: 55699

nonNullable won't provent you from setting null but it changes behavior around defaultValue (also if you mess with the types you're on your own)

See https://angular.dev/api/forms/FormControlOptions#nonNullable

Upvotes: 1

Related Questions