Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MatchValidator broken due to CompoundWidget state #110

Merged
merged 1 commit into from Feb 17, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions tests/test_validation.py
Expand Up @@ -141,6 +141,24 @@ class FailWidget(twc.CompoundWidget):
except ValidationError as ve:
assert(ve.widget.error_msg == NeverValid.msgs['never'])

def test_compound_MatchValidator(self):
"""Test that compound widgets validate with MatchValidator"""
class MatchyWidget(twc.CompoundWidget):
one = twc.Widget(validator=MatchValidator('two'))
two = twc.Widget

try:
MatchyWidget.validate({'one': 'foo', 'two': 'foo'})
except ValidationError as ve:
assert False, "Widget should have validated correctly."

try:
MatchyWidget.validate({'one': 'foo', 'two': 'bar'})
assert False, "Widget should not have validated."
except ValidationError as ve:
pass


def test_compound_validation_formencode(self):
"""Test that compound widgets validate with formencode."""

Expand Down
12 changes: 12 additions & 0 deletions tw2/core/util.py
Expand Up @@ -129,6 +129,18 @@ def flush_memoization():
cb()


def clone_object(obj, **values):
if obj is None:
obj = type('_TemporaryObject', (object,), {})()
else:
obj = copy.copy(obj)

for k,v in values.items():
setattr(obj, k, v)

return obj


# relpath support for python-2.5
# Taken from https://github.com/nipy/nipype/issues/62
# Related to https://github.com/toscawidgets/tw2.core/issues/30
Expand Down
8 changes: 7 additions & 1 deletion tw2/core/validation.py
Expand Up @@ -616,7 +616,13 @@ def other_field_str(self):
return capitalize(util.name2label(self.other_field).lower())

def _validate_python(self, value, state):
if self.other_field not in state or value != state[self.other_field]:
if isinstance(state, dict):
# Backward compatibility
values = state
else:
values = state.full_dict

if self.other_field not in values or value != values[self.other_field]:
raise ValidationError('mismatch', self)

def _is_empty(self, value):
Expand Down
2 changes: 2 additions & 0 deletions tw2/core/widgets.py
Expand Up @@ -665,6 +665,8 @@ def _validate(self, value, state=None):
any_errors = False
data = {}

state = util.clone_object(state, full_dict=value, validated_values=data)

# Validate compound children
for c in (child for child in self.children if child._sub_compound):
try:
Expand Down