Permalink
Browse files

When widget has key and so gets data by key validation was still retu…

…rning data by id. Now validation returns data by key when available. Also simplify CompoundWidget validation
  • Loading branch information...
amol- committed Mar 22, 2012
1 parent 56a6ba3 commit fa197ba30ace8540786f0ea79502074e5c66c15b
Showing with 33 additions and 22 deletions.
  1. +5 −1 tests/test_validation.py
  2. +1 −1 tests/test_widgets.py
  3. +27 −20 tw2/core/widgets.py
@@ -215,8 +215,12 @@ def test_compound_keyed_children(self):
inp = {'a': {'x':'test', 'y':'test2'}}
try:
compound_keyed_widget.validate(inp)
assert False
except twc.ValidationError, e:
assert "is not a valid OpenId" in str(e)
pass
cw = twc.core.request_local()['validated_widget']
assert "is not a valid OpenId" in cw.children.c.error_msg
def test_rw_pass(self):
testapi.request(1)
@@ -450,7 +450,7 @@ class T(wd.CompoundWidget):
i = T(id="test").req()
self.assert_(i.keyed_children)
data = dict(kct="foo", koct="bar")
expected = dict(ct="foo", oct="bar")
expected = dict(kct="foo", koct="bar")
res = i._validate(data)
self.assert_(res == expected, res)
@@ -607,38 +607,41 @@ def _validate(self, value, state=None):
if formencode:
catch = (catch, formencode.Invalid)
for c in self.children:
#Validate compound children
for c in (child for child in self.children if child._sub_compound):
try:
data.update(c._validate(value, data))
except catch, e:
if hasattr(e, 'msg'):
c.error_msg = e.msg
any_errors = True
#Validate non compound children
for c in (child for child in self.children if not child._sub_compound):
d = value.get(c.key, '')
try:
if c._sub_compound:
data.update(c._validate(value, data))
elif hasattr(c, 'id') and c.id not in self.keyed_children:
val = c._validate(value.get(c.id), data)
if val is not vd.EmptyField:
data[c.id] = val
val = c._validate(d, data)
if val is not vd.EmptyField:
data[c.key] = val
except catch, e:
if hasattr(e, 'msg'):
c.error_msg = e.msg
# TBD - removed after merge
#if hasattr(e, 'value'):
# c.value = e.value
if not c._sub_compound:
data[c.id] = vd.Invalid
data[c.key] = vd.Invalid
any_errors = True
for cid in self.keyed_children:
c = getattr(self.children, cid)
d = value.get(c.key)
if d and d is not vd.Invalid:
data[c.id] = c._validate(d, data)
if self.validator:
try:
data = self.validator.to_python(data)
self.validator.validate_python(data, state)
except catch, e:
error_dict = getattr(e, 'error_dict', {})
if not error_dict:
raise
for c in self.children:
if error_dict and getattr(c, 'id', None) in error_dict:
c.error_msg = error_dict[c.id]
data[c.id] = vd.Invalid
if getattr(c, 'key', None) in error_dict:
c.error_msg = error_dict[c.key]
data[c.key] = vd.Invalid
raise
if any_errors:
@@ -851,6 +854,10 @@ def post_define(cls):
cls.child = cls.child(children=cls.children)
cls.children = []
if getattr(cls, 'validator', None):
cls.child.validator = cls.validator
cls.validator = None
if not isinstance(cls.child, type) or \
not issubclass(cls.child, Widget):
raise pm.ParameterError("Child must be a widget")

0 comments on commit fa197ba

Please sign in to comment.