【问题标题】:How to add Validators like Reactive Forms for Another Model Data如何为另一个模型数据添加响应式表单等验证器
【发布时间】:2018-10-14 17:55:11
【问题描述】:

Model.ts

export class Model{
    name:string;
    phone:number;
}

registration.ts

import {Model} from './Model.ts';

export class RegistrationComponent{

    ngOnInit(){
        public objModel:Model;
    }
    saveClick(){

    }
}

registration.html

<input type="text" [(ngModel)]="objModel.name" />
<input type="phone" [(ngModel)]="objModel.phone" />
<button (click)="saveClick()">Save</button>

我正在使用 [(ngModel)] 创建一个带有外部模型控件的表单。我如何在角度 2 中添加所需的验证、最小长度(如反应式表单/模型驱动表单方法)。我这样做对吗..?

【问题讨论】:

    标签: angular forms angular2-forms angular-reactive-forms reactive


    【解决方案1】:

    你应该使用FormGroup。

    使用[formGroup]="myForm"。只有满足每个字段的验证器才能提交请求。后者通过formControlName 链接。在所有验证器都满意之前,该按钮将被禁用。

    registration.component.html:

    <form [formGroup]="registrationForm" (ngSubmit)="onSubmit()">
        <p class="form-group">
            <label for="username">Name</label>
            <input
                    id="name"
                    type="text"
                    name="name"
                    formControlName="name"
                    value=""
                    class="form-control"
            />
            <span class="invalid-feedback">
              Please provide a valid name.
            </span>
        </p>
        <p class="form-group">
            <label for="phone">Telephone</label>
            <input
                    id="telephone"
                    type="text"
                    name="telephone"
                    formControlName="telephone"
                    value=""
                    class="form-control"
            />
            <span class="invalid-feedback">
              Please provide a valid telephone number.
            </span>
        </p>
        <p>
            <button
                    type="submit"
                    class="btn btn-primary btn-block"
                    [disabled]="!registrationForm.valid"
                    translate
            >Sign In</button>
        </p>
    </form>
    

    registration.component.ts:

    import { Component, OnInit } from '@angular/core';
    import { FormGroup, FormControl, Validators } from '@angular/forms';
    
    export interface Model {
        name?: string;
        telephone?: string;
    }
    
    @Component({
        selector: 'app-registration-form',
        templateUrl: './registration.component.html'
    })
    export class RegistrationComponent implements OnInit {
    
        registrationForm: FormGroup;
    
        ngOnInit() {
            if (
                this.registrationForm === undefined ||
                this.registrationForm === null
            ) {
                this.registrationForm = new FormGroup( {
                    name: new FormControl('', Validators.required, Validators.minLength(1)),
                    telephone:new FormControl('', Validators.required, Validators.minLength(1))
                });
            }
        }
    
        onSubmit() {
    
            if ( this.registrationForm.valid ) {
    
                const formValue = this.registrationForm.value;
    
                const model: Model = {
                    name: formValue.name,
                    telephone: formValue.telephone
                };
    
                console.log('model', model);
            }
        }
    
    }
    

    您可以阅读更多here。您可以使用任何类型的验证器,也可以构建自己的验证器。例如,您可以对您的电话号码应用一个正则表达式,以使用 Validators.Pattern('yourRegxTelephonePattern') 了解输入是否有效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多