Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FormField] Class does not reset properly with submit buttons #8848

Closed
bmbell opened this issue Dec 6, 2017 · 5 comments
Closed

[FormField] Class does not reset properly with submit buttons #8848

bmbell opened this issue Dec 6, 2017 · 5 comments
Assignees

Comments

@bmbell
Copy link

bmbell commented Dec 6, 2017

Bug, feature request, or proposal:

Bug

What is the expected behavior?

The mat-form-field-invalid class should be removed when calling myFormGroup.reset(), since this class normally only appears when the form control is invalid and touched. This does occur if I use a button of type="button" and not type="submit".

What is the current behavior?

The mat-form-field-invalid class does not reset properly after calling myFormGroup.reset() after a submit button is triggered.

What are the steps to reproduce?

http://plnkr.co/edit/QGtqH8DDylViivcny1BA?p=preview

Steps to reproduce:

Scenario 1

  1. Type anything into input field
  2. Click the "Clear" button
  3. Observe that the field is cleared and no form validation errors appear (it was reset properly)

Scenario 2

  1. Now, type anything into the input field again
  2. Click the "Submit" button (which is a submit button that calls formGroup.reset()
  3. Observe that the field is cleared, but form validation triggers

Also note the value of mat-form-field-invalid in both scenarios.

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

It is often common to reuse a form when entering multiple items into a system.

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

Tested using Angular 5.0.0, Material 5.0.0-rc.2, TypeScript 2.4.2

Is there anything else we should know?

@julianobrasil
Copy link
Contributor

You need to use resetForm() of the FormGroupDirective.

@bmbell
Copy link
Author

bmbell commented Dec 8, 2017

I have access to the FormGroup. How would I get access to the FormGroupDirective?

@julianobrasil
Copy link
Contributor

julianobrasil commented Dec 8, 2017

One of the ways can be seen in @willshowell's answer over here (I like this way): #4190 (comment)

Or you can also use:

<form #f="ngForm" (ngSumbit)="doAnything(f)">

</form>

and in typescript:

doAnything(f: FormGroupDirective) {

}

@andrewseguin andrewseguin changed the title The mat-form-field-invalid class does not reset properly with submit buttons [FormField] Class does not reset properly with submit buttons Dec 12, 2017
@mmalerba
Copy link
Contributor

mmalerba commented Jan 9, 2018

Please keep GitHub issues for bug reports / feature requests. Better avenues for troubleshooting / questions are stack overflow, gitter, mailing list, etc.

@mmalerba mmalerba closed this as completed Jan 9, 2018
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 8, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants