"AFTER RESETTING ANGULAR FORM I AM GETTING RED INVALID FORM" Code Answer's

You're definitely familiar with the best coding language TypeScript that developers use to develop their projects and they get all their queries like "AFTER RESETTING ANGULAR FORM I AM GETTING RED INVALID FORM" answered properly. Developers are finding an appropriate answer about AFTER RESETTING ANGULAR FORM I AM GETTING RED INVALID FORM related to the TypeScript coding language. By visiting this online portal developers get answers concerning TypeScript codes question like AFTER RESETTING ANGULAR FORM I AM GETTING RED INVALID FORM. Enter your desired code related query in the search bar and get every piece of information about TypeScript code related question on AFTER RESETTING ANGULAR FORM I AM GETTING RED INVALID FORM. 

AFTER RESETTING ANGULAR FORM I AM GETTING RED INVALID FORM

By Dull DuckDull Duck on Feb 24, 2021
By default, Angular/Material watch formControl's error state not only by touched but also submitted status of the form, see below source code.

isErrorState(control: FormControl | null, form: FormGroupDirective | NgForm | null): boolean {
  return !!(control && control.invalid && (control.touched || (form && form.submitted)));
                                                                       ^^^^^^^^^^^^^^
}
For reason above, you also need to reset the form to unsubmitted status.

You can call resetForm of FormGroupDirective(getting instance by ViewChild) for it will reset both the form data and submit status.

<form #form="ngForm" [formGroup]="checkForm" (ngSubmit)="submitForm(form)">
  ...
</form>

@ViewChild('form') form;

submitForm() {
  this.form.resetForm();
  ...
}
You can also overwrite the default one via implementing ErrorStateMatcher with custom conditions such as ignore submitted status of the form, and apply it on material components.

<mat-form-field>
  <input matInput formControlName="name" placeholder="name" [errorStateMatcher]="errorMatcher" />
  <mat-error *ngIf="checkForm.get('name').errors?.required">
    Name is required.
  </mat-error>
</mat-form-field>

// define custom ErrorStateMatcher
export class CustomErrorStateMatcher implements ErrorStateMatcher {
  isErrorState(control: FormControl, form: NgForm | FormGroupDirective | null) {
    return control && control.invalid && control.touched;
  }
}

// component code
@Component({...})
export class CustomComponent {
  // create instance of custom ErrorStateMatcher
  errorMatcher = new CustomErrorStateMatcher();
}
see fixed demo.

Share
Improve this answer
Follow
edited Jan 29 '19 at 3:35
answered Apr 12 '18 at 5:58

Pengyy
31.9k1515 gold badges6969 silver badges6868 bronze badges
Thanks for the solution. In the return statement of isErrorState(), both touch and form submitted are connected by "or" operator so only one of them need to be true at one point of time. I think builtIn function "checkForm.reset()" internally untouched, pristine all the fields. – Ajay Apr 12 '18 at 7:54
1
@Ajay Mention expression (control.touched || (form && form.submitted), either touched or form submitted will turn above expression to true. – Pengyy Apr 12 '18 at 8:19 
I am facing issues with above code. It show fields after focussing on them. Any hint would be helpful. – Ajay Apr 17 '18 at 4:03 
Fields are hidden in starting(similar to invisible). Shown only when user focusses on them. – Ajay Apr 17 '18 at 4:23
@Ajay I didn't see it in the demo I provided. Maybe you forgot to set placeholder for them? – Pengyy Apr 17 '18 at 4:35
Show 5 more comments

16

All you need to do is avoid (ngSubmit) method on your reactive form:
1. Remove (ngSubmit)="submitForm()" from your form tag
1. Change your form button type from 'submit' to 'button'
2. On this button you want to set a click action to (click)="submitForm()"
3. in the submitForm() method do:

this.checkForm.markAsPristine();
this.checkForm.markAsUntouched();
This will submit the form and reset its value without alerting validators

Source: stackoverflow.com

Add Comment

0

All those coders who are working on the TypeScript based application and are stuck on AFTER RESETTING ANGULAR FORM I AM GETTING RED INVALID FORM can get a collection of related answers to their query. Programmers need to enter their query on AFTER RESETTING ANGULAR FORM I AM GETTING RED INVALID FORM related to TypeScript code and they'll get their ambiguities clear immediately. On our webpage, there are tutorials about AFTER RESETTING ANGULAR FORM I AM GETTING RED INVALID FORM for the programmers working on TypeScript code while coding their module. Coders are also allowed to rectify already present answers of AFTER RESETTING ANGULAR FORM I AM GETTING RED INVALID FORM while working on the TypeScript language code. Developers can add up suggestions if they deem fit any other answer relating to "AFTER RESETTING ANGULAR FORM I AM GETTING RED INVALID FORM". Visit this developer's friendly online web community, CodeProZone, and get your queries like AFTER RESETTING ANGULAR FORM I AM GETTING RED INVALID FORM resolved professionally and stay updated to the latest TypeScript updates. 

TypeScript answers related to "AFTER RESETTING ANGULAR FORM I AM GETTING RED INVALID FORM"

View All TypeScript queries

TypeScript queries related to "AFTER RESETTING ANGULAR FORM I AM GETTING RED INVALID FORM"

AFTER RESETTING ANGULAR FORM I AM GETTING RED INVALID FORM limit characters and have three dots after in angular 6 sts getting slow while pressing control key its getting abort when im trying to open the webcame using opencv getting started with pnp js NFS is reporting that your exports file is invalid. Vagrant does this check before making any changes to the file. Please correct the issues below and execute "vagrant reload": Invalid formula - Operator ">=" doesn't support DATE >= TEXT. Operator ">=" supports ANY >= ANY loop through form controls angular unique validator angular reactive form form validation in angular material example git remove commits from branch after push react make multiple fetch requests one after another how to update usequery after mutation in apollo client how to trigger ngmodelchange after typing is finished react routes not working after build Two sets of parentheses after function call after effects how to parent only one property was left pending after its run completion. which document is created by system analyst after the requirements are collected from various stakeholders how to css after elements for background overlays adobe after effects tutorial activate router angular angular font awesome angular for each angular how to iterate object how to implement read more and readless in angular how to run resize event only on client side angular how to navigate from one page to another in angular angular hide element from component when on certain page skip test angular An unhandled exception occurred: Schematic "Module" not found in collection "@schematics/angular". pass data in route angular output events in angular with asynchronous angular 7 for loop index ts conditional (click) action angular angular post request angular array filter typescript angular send mailto html angular material import chip Angular forkjoin on button click scroll to div angular amcharts angular universal angular set query params ERROR in The Angular Compiler requires TypeScript >=3.4.0 and angular get url params pathmatch angular angular dictionary angular unsubscribe from observable install aos angular 10 orderBy firebase angular angular 8 set cookie to string angular elementref parent conditional inline style angular mat-nav-list' is not a known element angular 9 angular modal dismisss how to navigate without realoding angular angular calculate difference between two dates angular append array to another import hostlistener angular how to create app.routing.module.ts in angular 6 remove white border around components angular angular unsubscribe example angular firestore timestamp date pipe get angular width get data from json file angular length array angular if image is broken show alternative image angular validation maxlength angular error TS2307: Cannot find module '@angular/cdk/bidi'. property 'do' does not exist on type 'observable'. angular 9 angular reload component on route param change what is typescript in angular angular viewchild an argument for opts was not provided angular typescript filter array group by attribute add class to element angular in ts angular start date end date validation cannot find module 'typescript' angular 9 .find angular how does it work angular closest element create class angular adding Angular routing pie chart typescript code in angular angular typescript refresh page share data between components angular angular redirect on submit convert all properties, object, array to camal case in angular js links a otros components angular angular from date to date validation angular currency pipe pt-br as variable angular ngfor conditional pipe Cannot find module '@azure/msal-angular' or its corresponding type declarations.ts(2307) angular date to string format angular conditional pipe angular prevent double click angular no internet detection conditional src angular make component using angular cli angular pipes stackoverflow ngbdate angular angular innerhtml style install dependencies angular from date and to date validation in angular 8 angular error TS2532: Object is possibly 'undefined'. angular make variable optional validation minlength angular How to add new row to a particular index of a ag grid using angular 7 Angular 8 ngClass If angular No provider for HttpClient maxlength reach focus on next input angular expected 2 arguments but got 1. viewchild angular ERROR in The Angular Compiler requires TypeScript >=4.0.0 and Cannot find module '@angular/core' or its corresponding type declarations Cannot find module '@angular/material/radio' or its corresponding type declarations. font awesome angular angular get url param enabletrace angular angular 8 ts refresh page angular material button css not working set default route angular angular scroll to top prevent row click event when button is clicked angular html scroll to top angular angular email regular expression angular change how date looks The Angular CLI process did not start listening for requests within the timeout period of 0 seconds. @angular/fire/angularfire2.d.ts:37:49 - error TS2344: Type 'T[K]' does not satisfy the constraint '(...args: any) => any angular get current date yyyy-mm-dd angular onclick toggle class No provider for ChildrenOutletContexts Angular angular host binding class on input angular change element style on click angular typescript set meta data conditional style angular import lodash issue angular angular formgroup validate manually pass data through router angular angular formgroup mark as touched how to get ip address in angular 8 email validation pattern angular angular convert boolean to string angular innerhtml style not working angular tooltip create split in angular 8 clean up an angular subscription angular set url parameters angular best practices unsubscribe angular shared animation Angular 9 : Error NG2003: No suitable injection token for parameter 'url' of class 'DataService'. Found string angular 6 checkbox checked dynamically execute only one test spec with angular-cli (ng test) firebase not found in envirorment.ts file angular create mock promise angular how to convert a normal app to a Angular Universal Module '"../node_modules/@angular/material/core"' has no exported member 'MAT_HAMMER_OPTIONS'. commands.reduce is not a function angular router html special characters decode angular This expression is not callable. typescript angular 11 socket.io angular delegate angular tokenize hwo to get query result in appolo angular angular material chips autocomplete example eliminate border white around components angular searching filtering ibraries in angular disable pdf download button in iframe in angular 10 how to store image in realtime firebase using angularfire2 and angular 8 firebase angular assets not showing why do we use #Email in angular with ngmodel how to set value of multiselect dropdown for reactive forms in angular 6 angular separate material module outputs i angular link to other components angular angular timestamp component how to reload same page in angular 8 angular minus date communication between components in angular mat expansion panel angular ionic 3 angular replacements or alternatives injection of generic services in angular from date and to date validation in angular 9 update object in array in ngxrx store in angular how to restrict alphabets in input field in angular angular type of string subscribe in angular 10 How to implement Bootstrap 4 for Angular 2 ngb-pagination angular numbers only directive angular conditional directives angular input change event datatype typescript Can't bind to 'formGroup' since it isn't a known property of 'form' mat-form-field must contain a MatFormFieldControl mat-form-field must contain a matformfieldcontrol checkbox reactive form programmatically set value React Typescript form event form control adding disabled and validators ionic 4 reset form mat-form-field email validation empty form elements except jquery 'mat-form-field' is not a known element 'mat-form-field' is not a known element:

Browse Other Code Languages

CodeProZone