title: Code architecture - Flask - Where to put form validation from database?
body: <p>I'm wondering where should I put a validation form which accessing database.
  Basically I will need user to enter <code>item_type</code> and I want to check first i the <code>item_type</code> has exist in database.</p>
  
  <p>There are 3 options:</p>
  
  <ol>
  <li><p>In the database model, I have <code>ItemType</code> class and I put function <code>add()</code> which will check if existing item has exist or not</p></li>
  <li><p>In view, so in the route of the page, from wtforms <code>form.validate_on_submit()</code>, I do a check to get data from database, and if exist I will put error in here</p></li>
  <li><p>In wtforms <code>validate()</code>, adding extra validation after default validation of the <code>Form</code> class</p></li>
  </ol>
  
  <p>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?</p>