sorry...got a little crazy with the copy and past.  This one should
look cleaner:

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
    """
    # where to upack args passed to __init__
    __unpackargs__ = ('*','field_names')
    #error message
    messages = {'invalid':"%(fst)s %(lst)s not unique"}
    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('invalid', state,
                               fst=fname, lst=lname)
            raise Invalid(msg, field_dict, state,
                          error_dict={'form':msg})
        return field_dict

    def _valid_name(self, fname, lname):
        '''check the database here'''
        return not (fname, lname) in [
            ('Speed', 'Racer'),
            ('Racer', 'X'),
            ('Harvey', 'Goldsmith')
            ]
--~--~---------~--~----~------------~-------~--~----~
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