Angular 7 - 提交表单时如何在表单的子组件中显示错误消息

标签 angular forms submit formgroups

我正在使用 Angular 7Angular Material 实现一个注册页面。在此页面中,我使用了一个密码输入组件,该组件获取对使用的 FormGroup 的引用。外部组件处理逻辑部分(例如 FormGroup 的外观、FormControl 的定义方式以及每个输入的验证方式)。当我点击密码输入并点击其他地方/输入任何无效内容时,输入的错误消息会正确显示。但是当我点击提交按钮而不触及任何输入字段时,FormGroup 的 FormControl 被验证(FormControl 甚至说它有错误)但没有被触及,所以没有 显示错误消息。

我已经查找了如何解决此行为。有些人建议在单击提交按钮后手动将所有表单控件设置为已触摸。是的,这行得通,但我希望有一个通用的解决方案,这样我以后就不必为每一个表单都这样做。

signup.component.ts:

export class SignupComponent implements OnInit {

    signupForm: FormGroup;

    constructor() { }

    ngOnInit() {
        this.signupForm = new FormGroup({
            'password': new FormControl('', [Validators.required]),
            'contact': new FormControl('', [Validators.required])
        });
    }

    onSubmit() {
        console.log('Form validity: ' + this.signupForm.valid);
    }

}

signup.component.html:

<mat-card class="signup-card">
    <h1 class="signup-title">Sign up</h1>

    <form [formGroup]="signupForm" #form="ngForm" (ngSubmit)="onSubmit()" fxLayout="column">
        <!-- Password -->
        <app-base-password-input [parentForm]="signupForm"></app-base-password-input>

        <!-- Contact -->
        <div class="signup-contact">
            <mat-checkbox formControlName="contact" required>We may contact you.</mat-checkbox>
            <mat-error *ngIf="form.submitted && signupForm.controls['contact'].hasError('required')">Please check this box.</mat-error>
        </div>

    <button class="signup-button" mat-raised-button color="primary" type="submit">Sign up</button>

    </form>
</mat-card>

app-base-password-input.component.ts:

export class BasePasswordInputComponent {

    @Input() parentForm: FormGroup;

    constructor() { }

}

app-base-password-input.component.html:

<mat-form-field [formGroup]="parentForm">
    <mat-label>Password</mat-label>
    <input matInput type="password" formControlName="password" required>
    <mat-error *ngIf="parentForm.controls['password'].hasError('required')">Password is required.</mat-error>
</mat-form-field>

我的期望:
当我在没有触摸/单击任何输入字段的情况下单击提交按钮时,每个输入都会显示一条错误消息(例如嘿,用户!我需要!)。

实际发生了什么:
当我单击提交按钮而不触摸/单击任何输入字段时,只有外部组件中定义的 FormControls 显示错误消息(本例:联系人复选框)。我的输入组件没有显示任何错误消息(本例:密码组件)。

我希望我发布了您回答我的问题所需的一切。如果还有什么不清楚的地方,我会更新帖子 :)

最佳答案

正如您所说,我知道的唯一方法是在 onSubmit() 方法中手动将每个 FormControl 设置为“已触摸”。

SignupComponent.ts

onSubmit(){
    console.log('Form validity: ' + this.signupForm.valid);
    touchAllElements(this.signupForm);
}

touchAllElements(formGroup:FormGroup){
    Object.keys(formGroup["controls"]).forEach(element => {
        formGroup.get(element).markAsTouched();
    });
}

为避免在每个具有表单的组件中重复此方法,您可以将 touchAllElements 移至服务

关于Angular 7 - 提交表单时如何在表单的子组件中显示错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54416756/

相关文章:

javascript - Angular 4 通用延迟加载错误

node.js - 在企业环境中创建 Angular 项目(无互联网)

.net - 在绑定(bind)之前使用 MVC ModelBinders 过滤帖子值

javascript - 如何动态地将每个字段名及其各自的值添加到一个 json 对象中

php - 单击 PHP 页面上的提交按钮时执行多个 sql 查询

javascript - Angular HighCharts ParlimentChart 不工作

html - typescript 和 html toggleswitch 值

ruby-on-rails - Rails - 访问表单中的模型属性

提交表单的 Javascript 函数返回 bool 值

PHP:即使有多个具有相同名称的复选框,也将提交的数据保留在表单中