I'm wondering where should I put a validation form which accessing database.
Basically I will need user to enter item_type
and I want to check first i the item_type
has exist in database.
There are 3 options:
In the database model, I have
ItemType
class and I put functionadd()
which will check if existing item has exist or notIn view, so in the route of the page, from wtforms
form.validate_on_submit()
, I do a check to get data from database, and if exist I will put error in hereIn wtforms
validate()
, adding extra validation after default validation of theForm
class
I've seen people using number 2 and 3, but not sure which one is the best. The error message that I want will also need to display it on the specific field of the form (this is achieveable by method 2 and 3 since they have reference to the form field) but then again since it is related to accessing database, maybe it is better to put everything regarding database access to model functions?