Nil*_*ade 35 validation angular2-forms angular
我有以下Angular 2形式:
<register>
<form [ngFormModel] = "registrationForm">
<div class = "form-group">
<label class = "control-label" for="email">Email</label>
<input class = "form-control" type="email" id="email" ngControl="email" #email="ngForm">
</div>
<div *ngIf = "email.touched && email.errors">
<div *ngIf = "!email.errors.required && email.errors.underscoreNotFound" class = "alert alert-danger">
<span>Underscore is required</span>
</div>
<div *ngIf = "email.errors.required" class = "alert alert-danger">
<span>Email is required</span>
</div>
</div>
<div class = "form-group">
<label class = "control-label" for="password">Password</label>
<input class = "form-control" type="password" id="password" ngControl="password" #password="ngForm">
</div>
<div *ngIf = "password.touched && password.errors">
<div *ngIf = "password.errors.minLength && !password.errors.required" class = "alert alert-danger">
<span>Password should contain 6 characters</span>
</div>
<div *ngIf = "password.errors.required" class = "alert alert-danger">
<span>Password is required</span>
</div>
</div>
</form>
</register>
Run Code Online (Sandbox Code Playgroud)
这是我实现验证器的组件:
import {Component} from '@angular/core';
import {Control, ControlGroup, FormBuilder, Validators} from '@angular/common';
import {CustomValidator} from './CustomValidator';
@Component({
selector: 'register',
templateUrl: './app/authentication/register_validation/register.html',
})
export class RegisterComponent{
registrationForm: ControlGroup;
constructor(formBuilder:FormBuilder)
{
this.registrationForm = formBuilder.group({
email: ['',Validators.compose([Validators.required, CustomValidator.underscore])],
password: ['',Validators.compose([Validators.required,Validators.minLength(6)])]
});
}
}
Run Code Online (Sandbox Code Playgroud)
在这种形式中,email字段对两个验证器都正常工作,即当我不输入任何内容时,它会给出"Email is required"消息,当我开始输入内容时,它会给出"Underscore is required"消息,当我输入"_"所有错误消息时消失.但是,当我尝试在password现场应用这样的2个验证器时,它不起作用.当我不输入密码时,它会给出消息"Password is required".但是当我输入少于6个字符的内容时,minLength根本不显示消息.这段代码有什么问题?
cex*_*yat 76
该错误是关键minlength,而不是minLength:
<div *ngIf = "password.hasError('minlength') && !password.hasError('required')" class = "alert alert-danger">
<span>Password should contain 6 characters</span>
</div>
Run Code Online (Sandbox Code Playgroud)
Rem*_*tec 23
这真的让我感到困惑,因为我将我的标记中的密钥与我在代码中的密钥相匹配.
示例代码
password1: ['', [Validators.required, Validators.minLength(8)]],
样本标记
*ngIf="registrationRequest.get('password1').hasError('minlength')"
在代码中注意它minlength完全是小写的.
我面临着同样的问题,但经过如此多的研究,我得到了一个解决方案,请使用minlength insted of minLength 这是示例。
<div *ngIf = "password.errors.minlength && !password.errors.required" class = "alert alert-danger">
<span>Password should contain 6 characters</span>
</div>
Run Code Online (Sandbox Code Playgroud)
代替
<div *ngIf = "password.errors.minLength && !password.errors.required" class = "alert alert-danger">
<span>Password should contain 6 characters</span>
</div>
Run Code Online (Sandbox Code Playgroud)
希望这会帮助某人,谢谢
我遇到了同样的问题,我想验证一个包含年份的数字字段,所以我过去常常Validators.maxLength(4)确保有人不会在一年中意外输入 5 个数字。当您将输入类型设置为数字时,结果是:
<input type="number" formControlName='year'>
Run Code Online (Sandbox Code Playgroud)
那么 maxLength 不再“工作”了。如果你想半秒钟,这实际上是有道理的。
所以我将日期输入更改为:
year: [null, [Validators.required, Validators.min(1990), Validators.max(2050)]],
Run Code Online (Sandbox Code Playgroud)
使用数字字段时,哪种方法是正确的。
| 归档时间: |
|
| 查看次数: |
33069 次 |
| 最近记录: |