Wednesday, June 27, 2012

Using subclass of RegexValidator for form field

Django 1.3.1
Python 2.7.1

I'm attempting to subclass RegexValidator in my app's validators.py to
supply specific 'regex' and 'message' attributes and finding that the
field validation I'm attempting does not work. I figure I don't quite
understand how to subclass this validator correctly, or else how to
implement it as a callable for the 'validators' parameter:


# app_name/validators.py
from django.core.validators import RegexValidator

class PayloadValidator(RegexValidator):
regex = 'x'
message = u'Valid payload strings contain the letter "x"'


# app_name/models.py
from app_name import validators

class Configuration(models.Model):
payload = models.CharField(max_length=64, help_text="Text string
to match in health check signature",
validators=[validators.PayloadValidator])
...


But when I submit the form, the Configuration instance validates and
saves if I enter 'abc' as a value.

The following use of URLValidator in my models appears to work
correctly, and I had figured I used these validators in the same
manner:

reference_url = models.URLField(verbose_name="reference URL",
blank=True, help_text="(Optional) Reference URL",
validators=[URLValidator])


Here, entering "foo" into the field for reference_url outputs a
validation error on the form if I enter "foo".

What am I missing? Thanks.

--
Darren Spruell
phatbuckett@gmail.com

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

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate