gareth.doutch schrieb:
> Hi,
> 
> How do I create a validator that can check an input value against
> currently stored values to ensure that duplicate entries do not occur?
> I saw a discussion on this previously, but it went off the list.

Use the Schema chained_validators to add a custom FormValidator that 
does the trick.

Something along these lines:


class MyValidator(FormValidator):

     messages = {
         'invalid': _("Invalid handle (handle is already used)"),
         }

     messages = {
         'user_name': _("Invalid user_name (user_name is already used)"),
         'email': _("Invalid emal (email is already used)"),
         'password': _("You need to give a password"),
         }

     def validate_python(self, form_value, state):
         import varms.model as m
         user_name = form_value['user_name'].strip()
         email = form_value['email_address'].strip()
         own_id = form_value['id']
         error_dict = {}
         try:
             b = m.User.by_user_name(user_name)
             if b.id != own_id:
                 error_dict.update({'user_name' : 
self.message('user_name', state)})
         except m.SQLObjectNotFound:
             pass

         try:
             b = m.User.by_email_address(email)
             if b.id != own_id:
                 error_dict.update({'email_address' : 
self.message('email', state)})
         except m.SQLObjectNotFound:
             pass


         if form_value.get('set_password', False)  or own_id == -1:
             password = form_value['password']
             if password is None or not password.strip():
                 error_dict.update({'password' : 
self.message('password', state)})
         raise Invalid(None, None, state, error_dict = error_dict)

     # for whatever reason, we _need_ the above method
     # as well as this one.
     def validate_partial(self, form_value, state):
         self.validate_python(form_value, state)

     validate_partial_form = True


class MySchema(validators.Schema):

    chained_validators = [MyValidator()]


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"TurboGears" 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/turbogears?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to