Validate or before_insert

I make code in to validate and insert a document from webform.

From the docs about doc events, my understanding is that validate will run first and then before_insert. But from the terminal I see that before_insert is running first then followed by validate.

Is this true?

Now if I want to set a field value or run a function after validation, which event should I use?

Thank you

1 Like

I encountered the same case. before_insert is run first.

Difference from my point of view: before_insert runs only once the document is created, validate runs every time you save and when the document is created as well.

These hooks make a lot of difference when used with web form.

I use both hooks to validate and set value to a document from value inputed from a webform.
It makes sense because validate runs the last so the value saved can be sure is validated.
What confused me at first was the name and process flow:
webform → validate → ready to insert → inserting → recorded in doctype (db)
(this was I thought)