Eoghan Murray
2009-02-21 23:40:57 UTC
I've gotten so far with trying to do conditional/alternate validation,
but I think I've tied myself in a knot so better ask.
My departure point is using 'chained_validators' to do conditional
validation in a schema i.e.
chained_validators = [RequireIfPresent(present='mycheckbox',
required='mydatefield')]
The first complication is that 'mycheckbox' is actually a radio item.
I've rewritten 'RequireIfPresent' to accept a function so that I can write:
chained_validators = [RequireWhenCheck(check=lambda field_dict:
field_dict['myradio']=='required_value', required='mydatefield')]
This seems to be working okay.
The second (orthogonal) complication is that 'mydatefield' needs to be
converted to a date using DateConverter, but only when 'myradio'
=='required_value' - any value in it should be ignored completely
otherwise.
Any ideas how I can achieve this? Is there a way to switch between
alternate schemas based on e.g. the value of a radio item... i.e. am I
doing this wrong?
Thanks!
Eoghan
----
outline of implementation of RequireWhenCheck:
class RequireWhenCheck(FormValidator):
"""
pass in a function to test the field_dict
"""
check = None
required = None
validate_partial_form = True
messages = {
'empty': "Please enter a value",
}
def validate_partial(self, field_dict, state):
if self.check and self.check(field_dict):
if not field_dict.get(self.required):
raise Invalid('You must give a value for %s' %
self.required,
field_dict, state,
error_dict={self.required:
Invalid(self.message(
'empty', state), field_dict, state)})
but I think I've tied myself in a knot so better ask.
My departure point is using 'chained_validators' to do conditional
validation in a schema i.e.
chained_validators = [RequireIfPresent(present='mycheckbox',
required='mydatefield')]
The first complication is that 'mycheckbox' is actually a radio item.
I've rewritten 'RequireIfPresent' to accept a function so that I can write:
chained_validators = [RequireWhenCheck(check=lambda field_dict:
field_dict['myradio']=='required_value', required='mydatefield')]
This seems to be working okay.
The second (orthogonal) complication is that 'mydatefield' needs to be
converted to a date using DateConverter, but only when 'myradio'
=='required_value' - any value in it should be ignored completely
otherwise.
Any ideas how I can achieve this? Is there a way to switch between
alternate schemas based on e.g. the value of a radio item... i.e. am I
doing this wrong?
Thanks!
Eoghan
----
outline of implementation of RequireWhenCheck:
class RequireWhenCheck(FormValidator):
"""
pass in a function to test the field_dict
"""
check = None
required = None
validate_partial_form = True
messages = {
'empty': "Please enter a value",
}
def validate_partial(self, field_dict, state):
if self.check and self.check(field_dict):
if not field_dict.get(self.required):
raise Invalid('You must give a value for %s' %
self.required,
field_dict, state,
error_dict={self.required:
Invalid(self.message(
'empty', state), field_dict, state)})