单个字段的多个验证器

时间:2015-03-02 11:22:40

标签: python validation web2py

我有一个表nsksystem,它从另一个表nskrelease获取其两个字段nskmachinename和nskreleaseid的值。 要求是nsksystem.nskreleaseid + nsksystem.nskmachinename应该是唯一的,nsksystem.nskreleaseid来自nskrlease数据库。我没有在表格中强制执行任何约束。

db.define_table('nsksystem',
    Field('nskuserid',length=512,requires=IS_EMAIL(error_message='invalid email!'),default = auth.user.email if auth.user else None, label=T('Email ID'),writable=False),
    Field('nskmachinename', length=128, requires = IS_IN_DB(db,'nskrelease.nskname','%(nskname)s',error_message='Machine not registerd for release.'), label = T('Machine Name')),
    Field('nskpassword', 'password', length=512,readable=False, label=T('Machine Password')),
    Field('nskreleaseid',length=128, default='',label = T('Release'))
)

db.nsksystem.nskreleaseid.requires = [IS_IN_DB(db,'nskrelease.releaseid'), IS_NOT_IN_DB(db(db.nsksystem.nskmachinename == request.vars.nskmachinename), 'nsksystem.releaseid', error_message='Machine is already registered to the specified release.')]

在上面的代码中强制执行第一个要求,但我没有看到

的下拉列表
IS_IN_DB(db,'nskrelease.releaseid')

对于第二个要求,当我尝试提供冲突的输入而不是给我所需的错误时,会发出一张票。

1 个答案:

答案 0 :(得分:1)

如果将IS_IN_DB验证器放在列表中,它将不再生成选择窗口小部件。您可以使用_and参数:

,而不是将验证器放在列表中
db.nsksystem.nskreleaseid.requires = IS_IN_DB(db, 'nskrelease.releaseid',
    _and=IS_NOT_IN_DB(db(db.nsksystem.nskmachinename == request.vars.nskmachinename),
                      'nsksystem.releaseid',
                      error_message='Machine is already registered to the specified release.'))