看起来Web应用程序仍在应用默认的WTF表单验证,我该如何禁用它?基本上,子网字段正在执行我的自定义验证(参见image1):
但是,它似乎也应用了我不希望它做的默认验证(IP地址验证):
请告知我如何防止这种情况发生。请参阅下面的相关代码段:
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
...
...
答案 0 :(得分:2)
如果正则表达式匹配,则需要引发StopValidation()
方法。这将停止验证链中的任何进一步调用。
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 ...