0

我正在使用Angular Material (8). 我正在尝试为Validators表单上的每个字段编写正确的错误消息。截至目前,我收到以下与字段相关的错误。

在此处输入图像描述

为什么会在表单中发生这种情况?任何帮助、提示或建议将不胜感激!

TIA

文件:register.component.html

[... snip ...]

   <div class="flex-register-form">

        <!-- Form -->
        <form [formGroup]="registerForm" (ngSubmit)="onSubmit()" class="text-center">

            <div class="form-col">
                <div class="col">
                    <!-- First name -->
                    <div class="md-form">
                        <input required type="text" id="materialRegisterFormFirstName" class="form-control" mdbInput
                            formControlName="firstname" />
                        <label for="materialRegisterFormFirstName">First name</label>
                    </div>
                </div>
                <!-- Last name -->
                <div class="col">
                    <div class="md-form">
                        <input required type="text" id="materialRegisterFormLastName" class="form-control" mdbInput
                            formControlName="lastname" />
                        <label for="materialRegisterFormLastName">Last name</label>
                    </div>
                </div>
[... snip ...]

文件:register.component.ts

import { Component, OnInit } from '@angular/core';
import { RegisterModel } from '../../models/register.models';
import { FormBuilder, FormGroup, Validators, FormControl } from '@angular/forms';

@Component({
  selector: 'app-register',
  templateUrl: './register.component.html',
  styleUrls: ['./register.component.scss']
})

export class RegisterComponent implements OnInit {

  user : RegisterModel = new RegisterModel();

  registerForm : FormGroup = new FormGroup( {
      'firstname' : new FormControl( [Validators.required, Validators.maxLength(25)] ),
      'lastname'  : new FormControl( [Validators.required, Validators.maxLength(25)]),
      'email'     : new FormControl( [Validators.required, Validators.email, Validators.maxLength(25) ] ),
      'password'  : new FormControl( [Validators.required, Validators.minLength(8), Validators.maxLength(25)]),
      'agree'     : new FormControl( [Validators.required])
    });

  constructor( private formBuilder: FormBuilder ) {

  }

  ngOnInit() {

  }


onSubmit() {
    console.log(" was submited ");
  }

}
4

1 回答 1

1

您的代码中的问题在这里。它是读取Validators值作为默认值。

registerForm : FormGroup = new FormGroup( {
      'firstname' : new FormControl( [Validators.required, Validators.maxLength(25)] ),
      'lastname'  : new FormControl( [Validators.required, Validators.maxLength(25)]),
      'email'     : new FormControl( [Validators.required, Validators.email, Validators.maxLength(25) ] ),
      'password'  : new FormControl( [Validators.required, Validators.minLength(8), Validators.maxLength(25)]),
      'agree'     : new FormControl( [Validators.required])
    });

FormControl用默认值初始化说null

'firstname' : new FormControl( null, [Validators.required, Validators.maxLength(25)] )

现在试试这个方法

registerForm : FormGroup = new FormGroup( {
    'firstname' : new FormControl( null, [Validators.required, Validators.maxLength(25)] ),
    'lastname'  : new FormControl( null, [Validators.required, Validators.maxLength(25)]),
    'email'     : new FormControl( null, [Validators.required, Validators.email, Validators.maxLength(25) ] ),
    'password'  : new FormControl( null, [Validators.required, Validators.minLength(8), Validators.maxLength(25)]),
    'agree'     : new FormControl( null, [Validators.required])
  });
于 2020-02-14T07:47:59.293 回答