How to handle error before insert in autoform hook?


#1

this is my example code

test = () ->
 alert "hello world"

AutoForm.hooks
 exampleId:
  before:
   insert: (doc) ->
    test()
    doc

i want to ask that is there anyway to prevent “before insert hook” not to run test function when there is an error when submit form. thanks in advance!


#2

The hook you’re looking for is “after”, not “before”. “before” is run before validation is done and is there to give you a chance to modify the form data before anything is going to be done with it.
“after” is where validation has already happened and the function arguments reflect that:

...
  after: {
    insert: function(err, id) {
      if (err) { ... error! ... return; }
      // validation and actual insert successful!
    }
  }
...

#3

thanks you i will try that :smile: