简体   繁体   English

Angular:不显示模板驱动的表单验证错误消息

[英]Angular: Template-driven form validation error messages won't display

I'm having trouble with the error messages on my Angular(v6) form validations. 我在Angular(v6)表单验证上遇到错误消息时遇到问题。 If a required field isn't filled in when the submit button is clicked, the form should not submit and there should be an error message saying that that field is required. 如果在单击提交按钮时未填写必填字段,则表单不应提交,并且应该显示一条错误消息,指出该字段是必需的。 This all works correctly except that the error message wont display. 这一切都正常工作,除了错误信息不会显示。 The input will highlight red, but that's it. 输入将突出显示红色,但就是这样。

I would like it to have a similar effect to this example 我希望它对这个例子有类似的效果

My form looks like this: 我的表单看起来像这样:

<form name="form" class="form-horizontal" (ngSubmit)="createMPForm.form.valid && createNewMonitoringPoint()" #createMPForm="ngForm"  novalidate>
            <div class="form-group">
                <table>
                    <tbody>                      
                        <tr>                                
                            <td class="left_td">
                                <p >Monitoring Point Name *</p>
                                <input type="text" name="name" class="col-md-12 form-control"
                                    #name="ngModel" id="name"           
                                    [ngClass]="{ 'is-invalid': createMPForm.submitted && name.invalid }" required
                                    [(ngModel)]="newmp.name" onfocus="this.placeholder = ''"
                                    placeholder="e.g., A123 Outfall NW">
                            </td>

                            <td class="left_td">
                                <p>Install Date *</p>
                                <input type="date" name="installDate" class="col-md-12 form-control"
                                    #installDate="ngModel" id="installDate"  [ngClass]="{ 'is-invalid': createMPForm.submitted && installDate.invalid }" required
                                    [(ngModel)]="newmp.installDate" onfocus="this.placeholder = ''">
                            </td>
                        </tr>
                        <tr>//can't get the below portion to work
                            <td>    
                                <div *ngIf="createMPForm.submitted && name.invalid" class="invalid-feedback">
                                   <div *ngIf="name.errors.required">
                                     <p class="text-danger left_td">Name is required</p>                        
                                   </div>
                                 </div>
                            </td>
                            <td *ngIf="createMPForm.submitted && installDate.invalid" class="invalid-feedback"> 
                                <div *ngIf="installDate.errors.required">
                                  <p class="text-danger left_td">Date of installation is required</p>                              
                             </td>                                 
                         </tr>
                     </tbody>
                </table>
                         <button type="submit" value="Add Site">Create New Monitoring Point</button>
            </div>
</form>

What might I be missing? 我可能会失踪什么?

site-settings.component.ts 网站settings.component.ts

import { Component, OnInit, Input, Inject } from '@angular/core';
import { AuthService } from "../../services/auth.service";
import { SiteService } from "../../services/site.service";
import { MonitoringPointService } from "../../services/monitoring- point.service";
import { Router, ActivatedRoute } from '@angular/router';
import { DateTime } from 'luxon';
import { DeviceService } from "../../services/device.service";
import { FormBuilder, FormGroup, Validators, FormControl } from '@angular/forms';
import { MatDialog, MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog';                           

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

export class SiteSettingsComponent implements OnInit {
  newmp = {
    name: "",
    installDate: ""
  }

constructor(public deviceService: DeviceService, private formBuilder: FormBuilder, public dialog: MatDialog, private router: Router, private route: ActivatedRoute, public authService: AuthService, public siteService: SiteService, public monitoringPointService: MonitoringPointService) { }

createNewMonitoringPoint() {
    this.monitoringPointService.createNewMonitoringPoint(this.newmp, this.authService.userSession.authToken)
      .subscribe(
        data => {
          alert('Monitoring Point was edited successfully')
        }
      )
 }

Some opened tags are not closed with their closinng tags. 一些打开的标签没有用他们的closinng标签关闭。 You have to close <table> , <tbody> and the <div> tag after this paragraph <p class="text-danger left_td">Date of installation is required</p> . 您必须在此段落之后关闭<table><tbody><div>标签<p class="text-danger left_td">Date of installation is required</p> Then your code will work properly. 然后你的代码将正常工作。

working stackblitz 工作stackblitz

I hope this HTML will work for you 我希望这个HTML对你有用

<form name="form" class="form-horizontal" (ngSubmit)="createMPForm.form.valid && createNewMonitoringPoint()" #createMPForm="ngForm" novalidate>
    <div class="form-group">
        <table>
            <tbody>
                <tr>
                    <td class="left_td">
                        <p>Monitoring Point Name *</p>
                        <input type="text" name="name" class="col-md-12 form-control" #name="ngModel" id="name" [ngClass]="{ 'is-invalid': createMPForm.submitted && name.invalid }" required [(ngModel)]="newmp.name" onfocus="this.placeholder = ''" placeholder="e.g., A123 Outfall NW">
                    </td>

                    <td class="left_td">
                        <p>Install Date *</p>
                        <input type="date" name="installDate" class="col-md-12 form-control" #installDate="ngModel" id="installDate" [ngClass]="{ 'is-invalid': createMPForm.submitted && installDate.invalid }" required [(ngModel)]="newmp.installDate" onfocus="this.placeholder = ''">
                    </td>
                </tr>
                <tr>//can't get the below portion to work
                    <td>
                        <div *ngIf="createMPForm.submitted && name.invalid" class="invalid-feedback">
                            <div *ngIf="name.errors.required">
                                <p class="text-danger left_td">Name is required</p>
                            </div>
                        </div>
                    </td>
                    <td *ngIf="createMPForm.submitted && installDate.invalid" class="invalid-feedback">
                        <div *ngIf="installDate.errors.required">
                            <p class="text-danger left_td">Date of installation is required</p>
                        </div>
                    </td>
                </tr>
            </tbody>
        </table>
        <button type="submit" value="Add Site">Create New Monitoring Point</button>
    </div>
</form>

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

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