Angular 2表单验证,minLength验证器不起作用

时间:2016-06-27 23:08:20

标签: validation angular angular2-forms

我遵循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>

这是我已实施验证器的组件:

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)])]
        });
    }

}

在这种形式中,email字段适用于两个验证器,即当我不输入任何内容时,它会给出"Email is required"消息,当我开始输入内容时,它会给出"Underscore is required"消息当我输入"_"时,所有错误消息都会消失。但是,当我尝试在password字段上应用这样的2个验证器时,它不起作用。当我不输入密码时,它会将消息显示为"Password is required"。但是当我键入少于6个字符的内容时,minLength消息根本不显示。这段代码有什么问题?

7 个答案:

答案 0 :(得分:64)

error key is minlength而非minLength

<div *ngIf = "password.hasError('minlength') && !password.hasError('required')" class = "alert alert-danger">
  <span>Password should contain 6 characters</span>
</div>  

答案 1 :(得分:19)

这真的让我感到困惑,因为我将我的标记中的密钥与我在代码中的密钥匹配不正确。

示例代码

password1: ['', [Validators.required, Validators.minLength(8)]],

示例加价

*ngIf="registrationRequest.get('password1').hasError('minlength')"

在代码中注意minlength完全是小写的。

答案 2 :(得分:2)

我遇到了同样的问题,我想验证包含年份的数字字段,因此我使用Validators.maxLength(4)来确保某人在一年中不会偶然输入5个数字。将输入类型设置为数字时结果为

<input type="number" formControlName='year'>

然后maxLength不再“起作用”。如果您考虑半秒钟,这实际上是有意义的。

所以我将日期输入更改为:

year: [null, [Validators.required, Validators.min(1990), Validators.max(2050)]],

使用数字字段时哪种方法是正确的。

答案 3 :(得分:1)

<div *ngIf = "password.errors.minLength && !password.errors.required" class = "alert alert-danger">
<span>Password should contain 6 characters</span>
</div> 

minLength 必须是小写的 minlength

答案 4 :(得分:0)

我也面临着同样的问题,但是经过如此多的研究,我找到了解决方案, 请使用由 minLength 插入的 minlength 这是示例。

<div *ngIf = "password.errors.minlength && !password.errors.required" class = "alert alert-danger">
      <span>Password should contain 6 characters</span>
    </div> 

代替

<div *ngIf = "password.errors.minLength && !password.errors.required" class = "alert alert-danger">
  <span>Password should contain 6 characters</span>
</div> 

希望这会对某人有所帮助,谢谢

答案 5 :(得分:0)

field: ['', Validators.compose([Validators.required, Validators.minLength(8)])]

这仅适用于多个验证器。

答案 6 :(得分:0)

这对我有用,用于密码验证。

<input type="password" class="form-control" name="password" placeholder="Password" [(ngModel)]="register.password" #password="ngModel" minlength="6" required>