1 Star 0 Fork 0

aspirefhaha / udisk_man_web

Create your Gitee Account
Explore and code with more than 6 million developers,Free private repositories !:)
Sign up
This repository doesn't specify license. Without author's permission, this code is only for learning and cannot be used for other purposes.
Clone or download
customValidators.py 1.00 KB
Copy Edit Web IDE Raw Blame History
aspirefhaha authored 2015-11-12 18:56 . still in test
#-*- coding: utf-8 -*-
from formalchemy import ValidationError
#检验两个字段的值是否相等
def customEqual(equal_field, message):
def check(value, field):
if field.is_required() or (not field.is_required() and value):
if getattr(field.parent, equal_field).value != value:
raise ValidationError(message)
return check
#检验字段值在数据库中是否唯一,并可设置是否忽略自身值,这在编辑已有信息的时候验证唯一非常有用
def customUnique(session, message="此数据已存在", checkSelf=False):
def check(value, field):
model = field.model.__class__
if checkSelf:
query = session.query(model).filter(
(getattr(model, field.name)==value) &
(model.id!=field.model.id)
).first()
else:
query = session.query(model).filter(getattr(model, field.name)==value).first()
if query:
raise ValidationError(message)
return check

Comment ( 0 )

Sign in for post a comment

1
https://gitee.com/fhaha/udisk_man_web.git
git@gitee.com:fhaha/udisk_man_web.git
fhaha
udisk_man_web
udisk_man_web
master

Search