question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

A required mat-input field does not show error when the parent form is submitted.

See original GitHub issue

Bug, feature request, or proposal:

For the purpose of code re-use and to maintain consistent functionality across my web app, I created an input component in my app whose role is to embed a mat-input in a parent form.

When the mat-input is a required field, the component displays it correctly in the parent form and an error is displayed when a user interacts with the field but does not enter anything. However, no error is displayed when the parent form is submitted without any user interaction with the embedded field.

Am I missing something? Is there a workaround?

What is the expected behavior?

An error should be displayed when the parent form is submitted.

What is the current behavior?

An error is not displayed when the parent form is submitted.

What are the steps to reproduce?

See: https://stackblitz.com/edit/angular-material2-issue-jph57r

What is the use-case or motivation for changing an existing behavior?

Which versions of Angular, Material, OS, TypeScript, browsers are affected?

I am using Chrome. Output of ng --version is given below:

Angular CLI: 1.6.0
Node: 8.9.3
OS: win32 x64
Angular: 5.1.0
... animations, common, compiler, compiler-cli, core, forms
... http, platform-browser, platform-browser-dynamic
... platform-server, router

@angular/cdk: 5.0.0
@angular/cli: 1.6.0
@angular/language-service: 4.4.6
@angular/material: 5.0.0
@angular-devkit/build-optimizer: 0.0.38
@angular-devkit/core: 0.0.25
@angular-devkit/schematics: 0.0.48
@ngtools/json-schema: 1.1.0
@ngtools/webpack: 1.9.0
@schematics/angular: 0.1.13
@schematics/schematics: 0.0.13
typescript: 2.4.2
webpack: 3.10.0

Is there anything else we should know?

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:5
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
Jefioziecommented, Feb 8, 2018

I have seen a issue about this somewhere but don’t remember where and what the original problem was. I fixed it by manual setting the fields as touched. See the code below.

workaround for me:

private setFormGroupTouched(formGroup: FormGroup) {
    Object.keys(this.formGroup.controls).forEach(field => {
      const control = this.formGroup.get(field);
      control.markAsTouched({ onlySelf: true });
    });
  }
2reactions
eprevotcommented, Nov 29, 2018

Does someone know what is the original problem ? I can use the workaround but I’d like to know why the mat-error doesn’t show on parent form submission, like it’s supposed to according to the documentation

Read more comments on GitHub >

github_iconTop Results From Across the Web

Angular Material: A required mat-input field does not show ...
The following workaround was posted by Jefiozie at GitHub. The code loops through all the controls for the form and marks them as...
Read more >
Input | Angular Material
matInput is a directive that allows native <input> and <textarea> elements to work ... with (touched) the element or the parent form has...
Read more >
Working with Angular 4 Forms: Nesting and Input Validation
markAsTouched() is used to make the form touched so we can display the errors at that moment. The buttons don't activate this property...
Read more >
Angular Custom Form Validators: Complete Guide
This post will cover the following topics: What is a form field validator? Custom Validators for reactive forms; Displaying error messages for ...
Read more >
Angular: how to do forms - Medium
Every form field is bound to a FormControl, and you get the field state or ... If we don't do this, global form...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found