django-floppyforms
django-floppyforms copied to clipboard
ModelForms appear to be ignoring model field validators
I've run into a case using a ModelForm where a field's validators are not being run.
For example:
models.py
import re
from django.core.validators import RegexValidator
from django.db import models
prefix_regex = re.compile(r'^[a-z]+[a-z0-9-]{3,61}$', re.IGNORECASE)
class Business(models.Model):
...
url_prefix = models.CharField(..., validators=[RegexValidator(regex=prefix_regex, message='Please enter a valid prefix')])
forms.py
import floppyforms as forms
from .models import Business
class PrefixForm(forms.ModelForm):
class Meta:
model = Business
fields = ('url_prefix', )
widgets = {'url_prefix': forms.TextInput}
Given this code, I wasn't getting any validation errors -ever-. I had to change the form to the following:
forms.py
import floppyforms as forms
from .models import Business, prefix_regex
class PrefixForm(forms.ModelForm):
url_prefix = forms.RegexField(..., regex=prefix_regex)
class Meta:
model = Business
fields = ('url_prefix', )
With this, I finally got an error of "Enter a valid value."
I couldn't find any documentation of this behaviour; only of the necessity to redefine widgets. Is there any way to get this validation to climb back up the chain, or do I need to redefine the validation for every model field using validators?