It's not about where to add the code. You are already doing the right thing if you see that the form fields get the red indicator that they are mandatory.
The problem is the meaning of "mandatory". The mandatory property is very limited in it's use: You can only use it for text fields, and, the state is only honored when you actually submit the form via a real forms submission. I assume you are just using a Doc.mailDoc() or mailForm() to email the filled out form back to you. That will ignore the mandatory flags. Does that mean they are useless? No, but you have to do the additional work of finding out if all your mandatory fields are filled in. There are plenty of examples on this site that show how to do that in a loop. Here is one such example: https://answers.acrobatusers.com/Loop-Through-Required-Fields-Submitting-Form-q277024.aspx
Karl Heinz Kremer
PDF Acrobatics Without a Net
PDF Software Development, Training and More...
http://www.khkonsulting.com