danyolgiax
danyolgiax

Reputation: 13086

Angular Material 2 md-select required not working on form submit

I create a simple form with a textbox and a dropdown. Both are required.

When I click submit button textbox becomes red while dropdown doesn't change.

Here the plunkr: https://embed.plnkr.co/oSe3JCMlB4jBNsjOgUoc/

I want both fields becames red when user click the submit button.

What I miss?

Upvotes: 2

Views: 1026

Answers (2)

0mpurdy
0mpurdy

Reputation: 3353

I believe this is because the required Validator checks to see if the FormControl is touched.

You can do this manually as described in this question

submit(form) {
    this.form.get('test2').markAsTouched()
    console.log(form)
}

It is also better to use (ngSubmit) instead of a click event on the submit button.

Live plunker example

Upvotes: 1

user844705
user844705

Reputation:

You have

'test1': new FormControl("", Validators.required)

in your code (select-form-example.ts) which is telling angular that it is a required field, hence the red box for failed validation. Try

'test1': new FormControl("")

Upvotes: 0

Related Questions