ForEach access to parent validators?

Jason Yamada-Hanff <[email protected]> Fri, 27 Feb 2009 09:43:09 -0500
Newsgroups gmane.comp.python.formencode
Message-ID <[email protected]>
Hi all,

I am trying to build a validator for checking unique constraints in
the db.  For this, I'd like access to the parent validator's field
dictionary from within a nested validator.  Is this possible?

A simple case: let's say I have two models with a one-to-many
relationship::

  Person
  ------
  id
  name

  Phone
  -----
  id
  person_id <fk: person.id> <UNIQUE #1>
  number <UNIQUE #1>

Phone has a compound unique constraint like (person_id, number).

The FormEncode schemas might look like this::

  class PhoneValidator(FancyValidator):
      id = Int()
      number = String()
  
  class PersonValidator(FancyValidator):
      allow_extra_fields = True
      filter_extra_fields = True
      pre_validators = [variabledecode.NestedVariables()]
      id = Int()
      phone = ForEach(PhoneValidator())

With this sort of scheme, I was hoping to just add a unique-ness
validator to chained_validators on the PhoneValidator.  To do actually
validate the constraint (person_id, number), though, I need to access
the value of `id` in PersonValidator from within the PhoneValidator
(within ForEach).  Adding a a `person_id` field to PhoneValidator
feels like a messy hack, since that information is already available
from the parent validator.

What is the best way to approach this with FormEncode?  I could place
the unique-ness check on chained_validators under PersonValidator, but
then don't I lose the ease of the ForEach wrapper?

Thanks,
Jason

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