angular: Empty controls are invalid when a [required]="false" property is defined
small example:
import {Component} from 'angular2/core'
import {FORM_DIRECTIVES, Control} from "angular2/common";
@Component({
selector: "login-comp",
directives: [FORM_DIRECTIVES],
template: '<input type="text" [(ngModel)]="login" [ngFormControl]="loginControl" [required]="isRequired()">'
})
export class App {
loginControl: Control = new Control('');
login:string = "";
isRequired(){
return false;
}
}
plnkr for alpha.54
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Reactions: 5
- Comments: 18 (8 by maintainers)
Stumbled over this a couple of days ago.
In my opinion this behavior perhaps should be reconsidered because it doesn’t feel one hundred percent naturally. When applying
Validators.requiredprogrammatically norequiredattribute is added in the DOM. Shouldn’t it be there for a semantic reason? I can see the advantage of not having to deal with browser default validation messages on submit, though.Otherwise as soon as you add
[required]in markup,Validators.requiredis implicitly set, no matter if you bind[required]="true"or[required]="false". Falling back to[attr.required]seems not intuitive when there is a[required]property at the same time.Should have been fixed by https://github.com/angular/angular/pull/11116.
Coming from large angular1 projects, it’s unacceptable, not to have a bindable [required] with working validation as pendant to ng-required.
@frickt
requiredis an attribute, you must addattrto it.