Using subclass of RegexValidator for form field

371 views
Skip to first unread message

Darren Spruell

unread,
Jun 27, 2012, 5:20:34 AM6/27/12
to django...@googlegroups.com
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
phatb...@gmail.com
Reply all
Reply to author
Forward
0 new messages