如何在Deform/Colander中使用错误消息进行简单的值检查

som*_*off 2 python pyramid deform colander

我正在Deform/Colander中实现一个简单的'tick to agree条款和条件框'.

因此,我只想检查该复选框是否有错误信息"你必须同意T&C".

我明白我可以用:

colander.OneOf([True]) 
Run Code Online (Sandbox Code Playgroud)

确保勾选方框.但是,OneOf不允许自定义错误消息.这样做的正确方法是什么?

Chr*_*ugh 5

使用自定义验证器:

def t_and_c_validator(node, value):
    if not value:
        raise Invalid(node, 'You must agree to the T&C')

class MySchema(colander.Schema):
    t_and_c = colander.SchemaNode(
                  colander.Boolean(),
                  description='Terms and Conditions',
                  widget=deform.widget.CheckboxWidget(),
                  title='Terms and Conditions',
                  validator=t_and_c_validator,
                  )
Run Code Online (Sandbox Code Playgroud)