Archimondain
Archimondain

Reputation: 424

Regex validator does not work with Django admin forms

I try to use a RegexValidator with a CharField, but I can't make it work...

class Configuration(models.Model):
    name = models.CharField(verbose_name=u'Name', validators =
           [RegexValidator(regex="[a-z]", message="Not cool", code="nomatch")])

I then just register it with

admin.site.register(Configuration)

But then in the admin forms, it accepts any possible name... Is the validation system suppose to work like that, or am I missing something ?

Upvotes: 1

Views: 780

Answers (1)

Alasdair
Alasdair

Reputation: 308799

Your current regex checks that your value contains a single character from a-z. So it allows a, but it also allows a1.

Try changing the regex to:

regex=r"^[a-z]+$"

By including ^ and $ to mark the beginning and end of string, you make sure that your string contains only characters from a-z. The + allows multiple characters.

Upvotes: 2

Related Questions