Reputation: 8127
I would like to get the value of an input in AutoForm.hooks
after
callback. This input is not part of my schema, it determines what template shows after form submission (and some other behind the scenes stuff). I'm creating the input by using plain html in my autoform template.
I tried
insert: function(error, result, template){
`$(template.firstNode).parent().find(".my-input").val()`
}
which appears to give me the default template html, not the live inputs.
What's the best way to get the value of this input?
UPDATE
I tried to grab the input value in the after
callback by simply using jQuery and selecting the input. However, the form clears via some method in autoform before the after callback is run, so that was a no-go.
I ended up storing this value globally from inside the before
callback, then checking that value in the after
callback.
This still seems like a hacky way to do things - I should be able to grab this value somehow, even if I have to create the input in a 'special' way with autoform.
Another issue is that I may have more than one of this form in the DOM at a time, so my solution would not cleanly work in that instance.
Upvotes: 3
Views: 452
Reputation: 1725
To prevent AutoForm from clearing out the inputs upon submission, you can specify resetOnSuccess=false
on the autoform template. You could then grab the value of the desired input in the hooks, and subsequently call AutoForm.resetForm(formId)
to manually clear out the form.
Upvotes: 1
Reputation: 138
I guess in this case you are doing something wrong. Using the autoform to store a kind of 'magic' sounds a little strange.
Isn't it possible to use e.g. the Session Object and store your data there or check if you could do it via special routes controller.
AutoForm.addHooks(['wordForm'], {
after: {
insert: function(error, word, template) {
if(!error){
url = Router.current().params;
Router.go('wordsList', {
languageId: url.languageId,
character: url.character});
Session.set('isModalShown', false);
}
},
update: function(error, word, template) {
if(!error){
url = Router.current().params;
Router.go('wordsList', {
languageId: url.languageId,
character: url.character});
Session.set('isModalShown', false);
}
},
}
});
Used this kind of approach to open/close a modal after saving and also "bind" this modal to a URL.
Upvotes: 0