如何使用angularJS2中的ReactiveFormsModule验证单选按钮?

时间:2017-01-30 10:29:10

标签: validation angular

我是angularJS2的新手,我正在尝试使用ReactiveFormsModule学习angularJS2形式的验证。现在我被困在如何验证angularJS2

中的单选按钮

这是我的组件代码

<div class="row">
    <div class="col-xs-2">your checkbox</div>
    <div class="col-xs-10">Your text</div>
</div>

HTML代码

import { Component, OnInit } from '@angular/core';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';

import { userDetails } from '../app/userDetails'

@Component({
    selector: 'user-validation',
    templateUrl: '../app/app.template.html'
})
export class userComponent implements OnInit {
    constructor(private fb: FormBuilder) { }
    userForm: FormGroup;
    users = new userDetails();
    ngOnInit(): void {
        this.buildForm();
    }

    buildForm(): void {
        this.userForm = this.fb.group({
            'username': [this.users.username, [
                Validators.required,
                Validators.minLength(4),
                Validators.maxLength(24)
            ]
            ]
        });

        this.userForm.valueChanges
            .subscribe(data => this.onValueChanged(data));

        this.onValueChanged(); // (re)set validation messages now
    }
    onValueChanged(data?: any) {
        if (!this.userForm) { return; }
        const form = this.userForm;

        for (const field in this.formErrors) {
            // clear previous error message (if any)
            this.formErrors[field] = '';
            const control = form.get(field);

            if (control && control.dirty && !control.valid) {
                const messages = this.validationMessages[field];
                for (const key in control.errors) {
                    this.formErrors[field] += messages[key] + ' ';
                }
            }
        }
    }
    onSubmit() {
        this.users = this.userForm.value;
    }
    formErrors = {
        'username': ''        
    };

    validationMessages = {
        'username': {
            'required': 'Name is required.',
            'minlength': 'Name must be at least 4 characters long.',
            'maxlength': 'Name cannot be more than 24 characters long.',
            'forbiddenName': 'Someone named "Bob" cannot be a hero.'
        }
    };
}

我无法在angularJS2中找到与单选按钮验证相关的任何文档,即使在angular.io

2 个答案:

答案 0 :(得分:3)

基于你的代码,你可以这样使用。

this.userForm = this.fb.group({
  'username': [this.users.username, [
     Validators.required,
     Validators.minLength(4),
     Validators.maxLength(24)
   ]],
  'gender': [this.users.gender, Validators.required]
});

HTML模板:

<form [formGroup]="userForm" (ngSubmit)="onSubmit()" class="form-group">
  <label>Gender</label>  
  <label><input type="radio" formControlName="gender" value="M" /> Male</label> 
  <label><input type="radio" formControlName="gender" value="F" />Female</label>
  <button type="submit" [disabled]="!userForm.valid" class="btn btn-success">Submit</button>
</form>

答案 1 :(得分:2)

请通过以下链接,它肯定会帮助您。 https://github.com/angular/angular/issues/7282

尝试将以下代码添加到组件代码

this.userForm = this.fb.group({
  'username': [this.users.username, [
     Validators.required,
     Validators.minLength(4),
     Validators.maxLength(24)
   ]],
  'gender': [this.users.gender, Validators.required]
});