简体   繁体   English

输入无效时出现Angular 5 FormBuilder错误未定义

[英]Angular 5 FormBuilder errors undefined when invalid input

[As a Newbie tried to put this into plnkr, but couldn't; [当新手尝试将其放入plnkr时,但是不能; problems getting @angular/forms added to json.] 将@ angular / forms添加到json时出现问题。]

Purpose: to iron out things I need to know to do all my work in FormBuilder HTML: 目的:整理在FormBuilder HTML中完成所有工作所需的知识:

  <input type="text"
         formControlName="bucket"
         value="A"
         [(ngModel)]="AllData.bucket" />

  // added to button  to test: [disabled]="this.myTestForm.invalid || this.myTestForm.pristine"
  <div><button
               type="submit">submit</button></div>
</form>

<div *ngIf="result.length > 0 ">{{result}}</div>
<div *ngIf="myTestForm.errors === true ">ERRORS FOUND</div>

Running the app: shows the formbuilder in the ts below initializes the input field correctly and if I add the [disabled] expression commented above it disables button correctly. 运行应用程序:在下面的ts中显示formbuilder会正确初始化输入字段,如果我在上面添加了注释的[disabled]表达式,则会正确禁用按钮。

Here's the ts: import {Component, OnInit} from '@angular/core'; 这是ts:从'@ angular / core'导入{Component,OnInit}; import {Validators, FormBuilder, FormGroup} from '@angular/forms'; 从“ @ angular / forms”导入{Validators,FormBuilder,FormGroup};

@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.css']
})
export class HomeComponent implements OnInit {
  myTestForm: FormGroup;
  result: string;
  AllData = {    //// wired to ngModel
    bucket: '12'
  }

  constructor(private fb: FormBuilder) {
    this.result = '';
  }

  ngOnInit() {
    this.myTestForm = this.fb.group({
      bucket: ['starting value', [Validators.required, Validators.minLength(5)]]  
<-- ngModel bucket above overrides this init value as expected -->

    });
  }

  onSubmit(value: any) { // ways to show results
    this.result = this.AllData.bucket;
    // OR //
    this.result = this.myTestForm.controls['bucket'].value;
    // OR //
    this.result = this.myTestForm.get('bucket').value;
  }
}

The app inits with '12' in the input field as expected. 应用程序按预期在输入字段中以“ 12”启动。 No matter what I put into the textbox before pressing the submit button, devTools always shows the myTestForm 'error' property as undefined. 无论我按提交按钮之前在文本框中输入了什么内容,devTools始终将myTestForm'error'属性显示为未定义。

I'm expected errors to have some sort of string(s) based on the type of error(s) that is occurring. 我期望错误根据正在发生的错误的类型而具有某种字符串。

Further, I scoured the web for ways to capture invalid fields as soon as the error occurs (of course for !pristine fields), but I couldn't get anything to work. 此外,我在网上搜索了如何在出现错误后立即捕获无效字段(当然是!pristine字段)的方法,但是我什么也无法工作。

Any help will be much appreciated. 任何帮助都感激不尽。

Thanks in advance, Chuck 预先感谢,查克

I have created a small demo to provide a suggestion on your approach 我创建了一个小演示 ,对您的方法提供了建议

  1. Do not use [(ngModel)] when your are using reactive forms approach, as ngModel will take precedence over the formControl and set its value to the control irrespective of formcontrol's value, that you have initialized. 使用反应式表单方法时,请勿使用[(ngModel)] ,因为ngModel将优先于formControl并将其值设置为该控件,而与已初始化的formcontrol'sformcontrol's

     <form [formGroup]="myTestForm" > <input type="text" formControlName="bucket" value="A" /> <div><button [disabled]="myTestForm.invalid || myTestForm.pristine" type="submit" >submit</button></div> </form> 
  2. To check form errors, use hasError() on controls 要检查表单错误,请在controls上使用hasError()

     <div *ngIf="myTestForm.get('bucket').hasError('required')">Input is required</div> <div *ngIf="myTestForm.get('bucket').hasError('minlength')">Min length should be 5</div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM