[FE-discuss] Alternate Schemas

Top Page
Author: Eoghan Murray
Date:  
To: formencode-discuss
Subject: [FE-discuss] Alternate Schemas

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)})



------------------------------------------------------------------------------
Open Source Business Conference (OSBC), March 24-25, 2009, San Francisco, CA
-OSBC tackles the biggest issue in open source: Open Sourcing the Enterprise
-Strategies to boost innovation and cut costs with open source participation
-Receive a $600 discount off the registration fee with the source code: SFAD
http://p.sf.net/sfu/XcvMzF8H
_______________________________________________
FormEncode-discuss mailing list
FormEncode-discuss@???
https://lists.sourceforge.net/lists/listinfo/formencode-discuss