Todd,

You're going to need to roll your own FormValidator, similar to
formencode.validators.RequireIfMissing.  Here's an example that you
can use as a starting point
from formencode import Invalid
from formencode.validators import FormValidator

class UniqueNameValidator(FormValidator):
    """checks if a first name, last name combo exists

    >>> unv = UniqueNameValidator('fname', 'lname')
    >>> unv.to_python({'fname':'Atticus', 'lname':'Finch'})
    {'lname': 'Finch', 'fname': 'Atticus'}
    >>> unv.to_python({'fname':'Racer', 'lname':'X'})
    Traceback (most recent call last):
        ...
    Invalid: The name Racer X already exists
    """
    # tells UniqueNameValidator where to unpack the args passed to
__init__
    __unpackargs__ = ('*','field_names')
    #error message
    messages = {'name_exists':"The name %(fst)s %(lst)s already
exists"}
    field_names = None

    def validate_python(self, field_dict, state):
        fname = field_dict[self.field_names[0]]
        lname = field_dict[self.field_names[1]]
        if not self._valid_name(fname, lname):
            msg = self.message('name_exists', state, fst=fname,
lst=lname)
            raise Invalid(msg, field_dict, state,
error_dict={'form':msg})
        return field_dict

    def _valid_name(self, fname, lname):
        '''This is where you would query the database to check for
existing names'''
        return not (fname, lname) in [
            ('Speed', 'Racer'), ('Racer', 'X'), ('Harvey',
'Goldsmith')
            ]

    def _valid_name(self, fname, lname):
        '''This is where you would query the database to check for
existing names'''
        return not (fname, lname) in self.names

You schema should look like this:

class AddSubmitterForm(BaseForm):
    first_name = formencode.validators.String(not_empty = True)
    last_name = formencode.validators.String(not_empty = True)
    phone = formencode.validators.PhoneNumber(not_empty = True)
    email = formencode.validators.Email(not_empty = True)
    chained_validators = [UniqueNameValidator('first_name',
'last_name')]

Hope that helps,

 - Kurt

On Mar 5, 11:39 am, Todd J <[EMAIL PROTECTED]> wrote:
> Hey everyone,
>
> I am trying to use a formencode Schema to validate a compound field.
> Basically I have this:
>
> class BaseForm(formencode.Schema):
>     allow_extra_fields = True
>     filter_extra_fields = True
>
> class AddSubmitterForm(BaseForm):
>     first_name = formencode.validators.String(not_empty = True)
>     last_name = formencode.validators.String(not_empty = True)
>     phone = formencode.validators.PhoneNumber(not_empty = True)
>     email = formencode.validators.Email(not_empty = True)
>
> I just want to ensure that the first_name AND the last_name are
> unique. So if 'Todd Johnson' is in the database already and I try to
> add 'Todd Johnson' it will not validate, but if I try to add 'Todd
> Smith' or 'Bob Johnson' that will validate.
>
> This is my first attempt at using formencode so I figure this is an
> elementary question, but I couldn't quite figure it out. Thanks in
> advance for your time and your help.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/pylons-discuss?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to