Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Validation Angular2 formBuilder-两个验证程序不';行不通_Validation_Angular_Formbuilder - Fatal编程技术网

Validation Angular2 formBuilder-两个验证程序不';行不通

Validation Angular2 formBuilder-两个验证程序不';行不通,validation,angular,formbuilder,Validation,Angular,Formbuilder,我有一个文本输入,应该是必需的,长度超过3个字符。当我点击它时,什么也不做(键入短于3个字符的内容)然后点击out,然后我添加一个红色边框,否则边框将是绿色的 export class RegisterpageComponent implements OnInit { userForm: any; constructor(private formBuilder: FormBuilder) { this.userForm

我有一个文本输入,应该是必需的,长度超过3个字符。当我点击它时,什么也不做(键入短于3个字符的内容)然后点击out,然后我添加一个红色边框,否则边框将是绿色的

    export class RegisterpageComponent implements OnInit {

          userForm: any;

          constructor(private formBuilder: FormBuilder) {
            this.userForm = formBuilder.group({
              login: ["", Validators.required, Validators.minLength(3)]
            }
          )
    }
在这里,我用表单连接上面的formBuilder,用输入连接验证器

  <form [ngFormModel]="userForm" (ngSubmit)="...">
    <input class="..." type="text" placeholder="..." ngControl="login" #login="ngForm" />
.
.
.
</form>
问题就在这里(我猜)
login:[“”,Validators.required,Validators.minLength(3)]

1*。当我单击输入时,通过单击out将其保留为空,然后我有红色边框-好。问题是无论我做什么,边框都是红色的,即使输入内容超过3个字符。为什么?

2*。登录验证程序为
login:[“”,Validators.minLength(3)]
当我单击输入时,通过单击out将其保留为空,然后我有了绿色边框。为什么?红色边框表示输入的内容超过3个字符(在这里验证程序工作)

3*。登录验证器为
[”,Validators.minLength(3)]
和HTML代码如
最终我得到了我想要的东西-当我单击输入时,通过单击out将其保留为空-红色边框(好),当我键入长度超过3个字符时-绿色边框(好)。为什么2*不能这样工作

最后-验证器设置的第一个参数是什么?谢谢大家!

尝试下一个:

this.userForm = formBuilder.group({
    login: ["", Validators.compose([ Validators.required, Validators.minLength(3) ])
});

像这样试试,对我有用

this.formvalid = new FormGroup({
  login: new FormControl('', [Validators.required, Validators.minLength(3)])
  }); 

<div [formGroup]=" formvalid">
<label id="label" >Cheque No</label>
  <span *ngIf="formvalid.controls.login.status == 'INVALID' && formvalid.touched" class="required" aria-required="true"> * </span>      
    <input type="text" formControlName="login" [(ngModel)]="login" class="form-control form-control-sm"  placeholder="login">        
</div>
this.formvalid=new FormGroup({
登录:新FormControl(“”,[Validators.required,Validators.minLength(3)])
}); 
支票号码
*       

在输入字段中,允许输入少于3个字符。但如果输入字段无效,则会通过指示span(*)来指示表单无效。

请以这种方式创建表单并更新组件,因为您已在异步验证程序中创建了minlength验证程序

 this.userForm = formBuilder.group({
   login: ["", [Validators.required,Validators.minLength(3)]]
 }

 <form role="form" [formGroup]="userForm" (ngSubmit)="onSubmit(userForm)">
     <div>
       <input formControlName="login">    
    </div>
</form>
this.userForm=formBuilder.group({
登录:[“”,[Validators.required,Validators.minLength(3)]]
}

什么是Angular2版本?旧表单还是新表单?Angular2 2.0.0-rc.4和0.2.0表单。我不知道它是新的还是旧的。我昨天刚在Angular2 API中找到formBuilder,它没有被弃用。如果使用
disableDeprecatedForms(),ProviderForms(),
并从
@angular/forms
导入表单内容,而不是
@angular/common
,然后您将使用新表单。现在,我正在尝试在@angular/forms上重写它(上面的示例基于@angular/common),我遇到了一个问题,
用于“登录”的无值访问器
“无值访问器”似乎与验证无关。我想有一个Plunker会很有帮助。我尝试过这个解决方案,但它只适用于所需的验证。我是否错过了什么。。?
 this.userForm = formBuilder.group({
   login: ["", [Validators.required,Validators.minLength(3)]]
 }

 <form role="form" [formGroup]="userForm" (ngSubmit)="onSubmit(userForm)">
     <div>
       <input formControlName="login">    
    </div>
</form>