Search code examples
angularformsvalidation

How to determine that a Angular form is tried to submit or not


How to determine that a Angular form is tried to submit or not by user to show some invalid fields information only when he attempted to do submission.


Solution

  • When you create a form in angular, then the form object contains a boolian property "submitted". It becomes true when user tries to submit a form.

    <form #searchForm="ngForm" [ngClass]="{'FormTried':searchForm.submitted}"
            (ngSubmit)="submitData(searchForm)" >
            // your form fields
    </form>
    

    I used it to add a class for form tried to submit or not.