【问题标题】:Web2py Custom ValidatorsWeb2py 自定义验证器
【发布时间】:2013-02-06 08:28:47
【问题描述】:

我是 Web2py 的新手,正在尝试使用自定义验证器。

class IS_NOT_EMPTY_IF_OTHER(Validator):

    def __init__(self, other,
                 error_message='must be filled because other value '
                               'is present'):
        self.other = other
        self.error_message = error_message

    def __call__(self, value):
        if isinstance(self.other, (list, tuple)):
            others = self.other
        else:
            others = [self.other]

        has_other = False
        for other in others:
            other, empty = is_empty(other)
            if not empty:
                has_other = True
                break
        value, empty = is_empty(value)
        if empty and has_other:
            return (value, T(self.error_message))
        else:
            return (value, None)

我不明白如何在我的桌子上使用它:

db.define_table('numbers',

    Field('a', 'integer'),
    Field('b', 'boolean'),
    Field('c', 'integer')

我想以一种在勾选“b”时不能将“c”保留为黑色的方式使用它。

【问题讨论】:

    标签: python web2py customvalidator custom-validators


    【解决方案1】:

    将代码保存在 /modules/customvalidators.py 上

    from gluon.validators import is_empty
    from gluon.validators import Validator
    
    
    class IS_NOT_EMPTY_IF_OTHER(Validator):
    
        def __init__(self, other,
                     error_message='must be filled because other value '
                                   'is present'):
            self.other = other
            self.error_message = error_message
    
        def __call__(self, value):
            if isinstance(self.other, (list, tuple)):
                others = self.other
            else:
                others = [self.other]
    
            has_other = False
            for other in others:
                other, empty = is_empty(other)
                if not empty:
                    has_other = True
                    break
            value, empty = is_empty(value)
            if empty and has_other:
                return (value, T(self.error_message))
            else:
                return (value, None)
    

    然后在models/db.py中

    from customvalidator import IS_NOT_EMPTY_IF_OTHER
    
    db.define_table("foo",
        Field('a', 'integer'),
        Field('b', 'boolean'),
        Field('c', 'integer')
    )
    
    # apply the validator
    db.foo.c.requires = IS_NOT_EMPTY_IF_OTHER(request.vars.b)
    

    另外,请注意,在没有上述验证器的情况下,它可以轻松完成。 忘记上面所有的代码,试试这个简化的方法

    版本 2:

    controllers/default.py
    
    def check(form):
        if form.vars.b and not form.vars.c:
            form.errors.c = "If the b is checked, c must be filled"
    
    def action():
        form = SQLFORM(db.foo)
        if form.process(onvalidation=check).accepted:
            response.flash = "success"
        return dict(form=form)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-28
      • 1970-01-01
      • 1970-01-01
      • 2012-08-11
      • 1970-01-01
      • 2014-01-02
      • 2019-01-07
      相关资源
      最近更新 更多