1

I'm trying to access the minlength error but when I log all the errors it isn't there.

This is my form control

title: new FormControl("", [Validators.minLength(10), Validators.required]),

Here I'm expecting to see both the required and minlength error.

ngOnInit(): void {
  console.log(this.data.get('title')?.errors)
}

but instead, I only see the required one.

The console.log result

Any ideas? Thanks!

1
  • If title value is empty then it will fire required when there is any value in title then only it will check for minLength. Commented Aug 29, 2021 at 5:02

1 Answer 1

4

According to Angular Validators minLength(),

The minLength validator logic is also not invoked for values when their length property is 0 (for example in case of an empty string or an empty array), to support optional controls.

You can use the standard required validator if empty values should not be considered valid.

Hence minLength validator only fires when title is not an empty string.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.