2

I'm implementing a simple 'tick to agree to terms and conditions box' in Deform/Colander.

So, I simply want to check that the box is checked and have an error message saying 'You must agree to T&C'.

I understand that I can use:

colander.OneOf([True]) 

to ensure the box is ticked. However, OneOf does not allow for a custom error message. What would the correct way to do this be?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
somewhatoff
  • 971
  • 1
  • 11
  • 25

1 Answers1

5

Use a custom validator:

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,
                  )
Chris McDonough
  • 2,479
  • 18
  • 18
  • Thanks Chris, that works well. I suggest that in a future release it might be worth considering a msg = 'Custom message' keyword for the OneOf validator, if this would not cause problems. – somewhatoff Jul 23 '11 at 11:27