jazzband / jazzband/django-floppyforms

ModelForms appear to be ignoring model field validators

Open
#85 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
836
Forks
150
PR merge metrics
No merged PRs in 30d

Description

I've run into a case using a ModelForm where a field's validators are not being run.

For example:

models.py

``` python
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

``` python
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

``` python
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?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.