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

http://plnkr.co/edit/nPlfg2uNAMYTkc5gBWTh?p=preview

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Reactions: 5
  • Comments: 18 (8 by maintainers)

Most upvoted comments

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.required programmatically no required attribute 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.required is 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.

Coming from large angular1 projects, it’s unacceptable, not to have a bindable [required] with working validation as pendant to ng-required.

@frickt required is an attribute, you must add attr to it.

... [attr.required]="isRequired()">