I'm trying to create a custom form field that will validate against
auth.user. The field should throw a validation error if the user does
not exist, otherwise the field should validate.

So far I have:

class ExistingUserField(forms.CharField):
    def clean(self, value):
        # Check if the user exists
        try:
            user = User.objects.get(username=value)
        except:
            # User does not exist
            raise forms.ValidationError('User %s not found' % value)
       return value

I then use this in a form as:

class MyForm(forms.Form):
    user = ExistingUserField(required=False)

This code works as expected when the user field is populated but does
not honour the "required=False" argument, when the user field is left
blank the error "User not found" is raised.

Do i need to check for all forms of None and blank strings? Is there a
better way?

Paddy

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to