- March 18, 2007
- Pre .96
- newforms validators
- 0 (after 2 ratings)
How to proceed to add a custom validator to a newforms field : you just need to create a new class derivated from forms.YourField with a custom clean method. Do not forget the line super(UserField, self).clean(value) ; in our case, it verifies the field attributes : min_length, max_length or required.
More explications (in French) : des validateurs personnalisés pour Django
1 2 3 4 5 6 7 8
class UserField(forms.CharField): def clean(self, value): super(UserField, self).clean(value) try: User.objects.get(username=value) raise forms.ValidationError("Someone is already using this username. Please pick an other.") except User.DoesNotExist: return value