Search code examples
javascriptmeteormeteor-autoform

How to reset Meteor AutoForm when page changed?


I have page with text fields, this fields are required. I click 'submit' with empty fields => fields with red border (because it is required). Then I change page and return back => borders are still shown. How to reset meteor AutoForm when page changed? Thanks.


Solution

  • If you want to clear existing validation errors, you need to call AutoForm.resetForm("form-id");. You could put this call inside the Template.myTemplate.onDestroyed function, which will be triggered when the template is removed from the DOM and destroyed, i.e. on route change.

    For instance:

    Template.myTemplate.onDestroyed(function () {
      AutoForm.resetForm("form-id");
    });
    

    Or you could use routing hooks (assuming you use Iron Router):

    var myResetFormFunction = function () {
        AutoForm.resetForm("form-id");
    };
    
    Router.onStop(myResetFormFunction, {
        only: ['routeOne']
    });