Permalink
Browse files

Support if_empty and let BoolValidator validate None to False.

  • Loading branch information...
Cito committed Jun 12, 2013
1 parent 30cb858 commit a9d48944a8aa70e2d162b85a154b314fe33c3c8e
Showing with 6 additions and 4 deletions.
  1. +3 −3 tests/test_validation.py
  2. +3 −1 tw2/core/validation.py
@@ -438,9 +438,9 @@ def test_required(self):
class TestBoolValidator(tb.ValidatorTest):
validator = BoolValidator
to_python_attrs = [{}, {}, {}, {}, {}, {}, {}, {}, {},]
to_python_params = ['on', 'yes', 'true', '1', 1, True, 'Yes', 'True', 'off']
to_python_expected = [True, True, True, True, True, True, True, True, False]
to_python_attrs = [{}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}]
to_python_params = ['on', 'yes', 'true', '1', 1, True, 'Yes', 'True', 'off', '', None]
to_python_expected = [True, True, True, True, True, True, True, True, False, False, False]
class TestOneOfValidator(tb.ValidatorTest):
validator = OneOfValidator
@@ -212,6 +212,7 @@ class Validator(six.with_metaclass(ValidatorMeta, object)):
}
required = False
strip = True
if_empty = None
def __init__(self, **kw):
for k in kw:
@@ -222,7 +223,7 @@ def to_python(self, value, state=None):
if self._is_empty(value):
if self.required:
raise ValidationError('required', self)
return None
return self.if_empty
if self.strip and isinstance(value, six.string_types):
value = value.strip()
value = self._convert_to_python(value, state)
@@ -398,6 +399,7 @@ class BoolValidator(RangeValidator):
msgs = {
'required': ('bool_required', _('You must select this'))
}
if_empty = False
def _convert_to_python(self, value, state=None):
return str(value).lower() in ('on', 'yes', 'true', '1', 'y', 't')

0 comments on commit a9d4894

Please sign in to comment.