Search code examples
pythonflask-sqlalchemywtformsflask-wtformsflask-admin

Override Default WTF forms validation for FlaskAdmin with SQLAlchemy


It looks like the web app is still applying the default WTF Form validation, how can I disable this please? Basically the subnet field is performing my custom validation (see image1):

enter image description here

However, it seems to also apply the default validation (IP Address validation) which I do not want it to do:

enter image description here

Please advise how I can prevent this. See the relevant code snippet below:

from sqlalchemy.dialects.postgresql import INET
 ...
 ...
 ...
app = Flask(__name__)
 ...
 ...
db = SQLAlchemy(app)
 ...
 ...

class Ipac(db.Model):
    __tablename__ = "ipac"
    id = db.Column('id', db.Integer, primary_key=True)
    subnet = db.Column('subnet', INET)
    gateway = db.Column('gateway', INET)
    vrip = db.Column('vrip', INET)
    serverid = db.Column('serverid', db.Integer, db.ForeignKey('server.id'))

    def __str__(self):
        return self.subnet  
 ...
 ...

class IpacView(ModelView):
    def subnet_format(form, field):
        if not re.match(r'\b(([0-9]|[0-9]{2}|2[0-5]{2}|2[0-4][0-9]|1[0-9]{2})\.){3}([0-9]|[0-9]{2}|2[0-5]{2}|2[0-4][0-9]|1[0-9]{2})\/(3[0-2]|2[0-9]|1[0-9]|[0-9])\b', field.data):
            raise ValidationError("Must be a valid subnet format!")

    column_display_all_relations = True
    column_labels = dict(subnet='Subnet', gateway='Gateway', vrip='VR IP')
    column_searchable_list = ('subnet', 'vrip', 'gateway', 'server.hostname')

    form_args = dict(
        subnet=dict(validators=[subnet_format])
        )

    def is_accessible(self):
        return login.current_user.is_authenticated
 ...
 ...

Solution

  • You need to raise the StopValidation() method if your regexp matches. This stops any further calls in the validation chain.

    from wtforms.validators import StopValidation
    
    # ....
    
    class IpacView(ModelView):
    
        def subnet_format(form, field):
            if not re.match(
                    r'\b(([0-9]|[0-9]{2}|2[0-5]{2}|2[0-4][0-9]|1[0-9]{2})\.){3}([0-9]|[0-9]{2}|2[0-5]{2}|2[0-4][0-9]|1[0-9]{2})\/(3[0-2]|2[0-9]|1[0-9]|[0-9])\b',
                    field.data):
                raise ValidationError("Must be a valid subnet format!")
            else:
                raise StopValidation()
    
        # etc  ...