title: How to perform Validation on Flask-admin fields
body: <p>I want to perform some validation on Flask Admin. The Flask app is connected to a backend postgreSQL DB and is using SQLAlchemy ORM.</p>
  
  <p>I want to be able to perform validation checks on two fields (lan_nics, wan_nics) in the ServerView(flask.ext.admin.contrib.sqla.modelView). Basically I want to ensure the sum of two fields are not greater than a certain number. Here is the form:</p>
  
  <p><a href="https://i.stack.imgur.com/73Ezv.jpg" rel="nofollow noreferrer">ServerView</a></p>
  
  <p>Here is the test_app code. I've tried to follow the documentation for flask-admin. I added a def and then referenced that def in the form_args for validators dictionary... but it doesn't seem to work. At the moment I'm just trying to ensure that both of the fields have a value less than 5.</p>
  
  <pre><code>from flask import Flask
  from flask.ext.admin import Admin
  from flask.ext.admin.contrib.sqla import ModelView
  from flask.ext.sqlalchemy import SQLAlchemy
  from wtforms.validators import ValidationError
  import psycopg2
  
  
  # Flask and Flask-SQLAlchemy initialization here
  
  app = Flask(__name__)
  db = SQLAlchemy(app)
  app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://postgres:postgres@127.0.0.1/testdb'
  app.secret_key = 'MySecret'
  conn = psycopg2.connect("dbname='testdb' user='postgres' host='127.0.0.1' password='postgres'")
  
  class Server(db.Model):
      __tablename__ = "server"
      id = db.Column('server_id', db.Integer, primary_key=True)
      model = db.Column('model', db.Unicode)
      lan_nics = db.Column('lan_nics', db.Integer)
      wan_nics = db.Column('wan_nics', db.Integer)
  
      def __str__(self):
          return self.model
  
  class Hardware(db.Model):
      __tablename__ = "hardware"
      id = db.Column('hardware_id', db.Integer, primary_key=True)
      model = db.Column('model', db.Unicode)
      max_nics = db.Column('max_nics', db.Integer)
  
      def __str__(self):
          return self.model
  
  
  class ServerView(ModelView):
  
      # Disable model creation
      can_create = True
  
      # Override displayed fields
      column_list = ('model', 'lan_nics', 'wan_nics')
  
      column_display_all_relations = True
      column_searchable_list = ('model')
      column_labels = dict(model='Model', lan_nics='LAN Nics',  wan_nics='WAN NICs')
      form_columns = [ 'model', 'lan_nics', 'wan_nics' ]
  
  
      def max_allowed(form, field):
          if field.data &gt; 5:
              raise ValidationError('Max number of interfaces exceeded')
  
      form_args = dict(
          lan_nics=dict(validators=[max_allowed]), 
          wan_nics=dict(validators=[max_allowed])
      )
  
  
  class HardwareView(ModelView):
      # Disable model creation
      can_create = True
  
      # Override displayed fields
      column_list = ('model', 'max_nics')
  
      column_display_all_relations = True
      column_searchable_list = ('model')
      column_labels = dict(model='Model', max_nics='Max Nics')
      form_columns = ['model', 'max_nics' ]
  
      def __str__(self):
          return self.model
  
  
  admin = Admin(app, name="MyApp1")
  # Add administrative views here
  admin.add_view(ModelView(Server, db.session))
  admin.add_view(ModelView(Hardware, db.session))
  
  app.run(port=8000, host="0.0.0.0")
  </code></pre>