Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular2 form "required" binding - validation not performed

I have problems with binding a dynamic "required" to an input. The input is always invalid and does not interact with the required attribute. Please have a look at my Plunkr. May be I'm missing something?

@Component({
  selector: 'my-app',
  template: `
  <form #heroForm="ngForm">
    Set Required <input type="checkbox" [(ngModel)]="req">
    <input #inp ngControl="something" type="text" [(ngModel)]="myModel" [required]="req" #spy><br>
    required: {{req}}<br>
    required attribute: {{!!spy.attributes.getNamedItem("required")}}<br>
    classes: {{spy.className}}<br>
  </form>
  `
})
like image 640
westor Avatar asked Jun 17 '26 01:06

westor


1 Answers

Currently required need to be added statically otherwise Angular won't initialize the validator

<input #inp ngControl="something" type="text" [(ngModel)]="myModel" required #spy><br>

This is planned to be changed.

like image 112
Günter Zöchbauer Avatar answered Jun 18 '26 16:06

Günter Zöchbauer



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!