Reputation: 41
We have a site with several AutoForms on different pages / routes. If a user triggers validation on a particular form and triggers some validation errors, and then leaves the page with that form, when they return to that page later on the data they have entered is gone, but any validation errors remain (fields are highlighted and validation error messages are shown).
How can we reset the form validation when a user leaves the form page?
I know we can use AutoForm.resetForm('our-form-id')
to do the reset, and this works in the console, but I can't find the proper hook to hang it on. The onStop
IronRouter hook for the page would seem to be the right place, but that triggers an error Can't call Tracker.flush while flushing.
If I wrap it in a setTimeout
with timeout 0, so it doesn't run until the next tick, it has no effect (presumably the template is destroyed by that point).
I would think when the template is destroyed the error state would be, too, but its not. Is there an undocumented way to "hard" reset errors even if an active template no longer exists?
We are using AutoForm 4.2.2.
Upvotes: 4
Views: 546
Reputation: 1176
You need to use your router's exit hook to resetValidation, but you have to first get the context for your form. Here is how it is done in FlowRouter:
FlowRouter.route('/edit/:_id', {
name: 'editPage',
action: function() {
BlazeLayout.render('appLayout', {main: 'editPage'});
},
triggersExit: [function() {
AutoForm.getValidationContext('edit-form-id').resetValidation();
}]
});
For more information on flowRouter triggers see https://github.com/kadirahq/flow-router#triggers
Upvotes: 0
Reputation: 1620
You can reset the validations using
AutoForm.getValidationContext('form-id').resetValidation();
Upvotes: 1