Opened 8 years ago

Last modified 8 months ago

#26834 assigned New feature

MinValueValidator/MaxValueValidator not forwarded to form field for ModelForm

Reported by: Sergei Maertens Owned by: Tobias Kunze
Component: Forms Version: dev
Severity: Normal Keywords: MaxValueValidator, MinValueValidator, ModelForm, IntegerField
Cc: Ülgen Sarıkavak Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX: no

Description

I just ran into a possible enhancement.

Consider the following model:

DEFAULT_RATING = 50
MAX_RATING = 100
MIN_RATING = 0


class KitReviewPropertyRating(models.Model):
    """
    Represents properties for a kit review rated on a scale from MIN_RATING to MAX_RATING
    """
    kit_review = models.ForeignKey('KitReview', related_name='ratings')
    prop = models.ForeignKey('KitReviewProperty')
    rating = models.PositiveSmallIntegerField(
        _('rating'), default=DEFAULT_RATING,
        validators=[MinValueValidator(MIN_RATING), MaxValueValidator(MAX_RATING)]
    )

and the matching ModelForm:

from django.forms.widgets import NumberInput


class RangeInput(NumberInput):
    input_type = 'range'


class KitReviePropertyRatingForm(forms.ModelForm):

    class Meta:
        model = KitReviewPropertyRating
        fields = ('id', 'prop', 'rating')
        widgets = {
            'rating': RangeInput(attrs={'max': MAX_RATING})
        }

As you can see, I have to specify the max widget attribute manually, otherwise the html input looks like this: <input type="range" name="rating" value="50" min="0">, instead of the expected <input type="range" name="rating" value="50" min="0" max="100">. I had expected the Min/MaxValueValidator to be 'forwarded' to the form field (via IntegerField.formfield method), but no such thing happens. #26786 does add the validators on the model level, based on the db connection used and the limits there, and only if no such validators were present yet.

I'd like to make it so that Min/MaxValueValidators are translated into min_value/max_value defaults for the IntegerField.formfield method, this would be more in line with the expected output and reduce the need to repeat yourself.

Change History (11)

comment:1 by Tim Graham, 8 years ago

Triage Stage: UnreviewedAccepted

I'm wary of the additional complexity, but can't say that the request is unreasonable.

comment:2 by Sagar Nilesh Shah, 8 years ago

Owner: changed from nobody to Sagar Nilesh Shah
Status: newassigned

comment:3 by Sagar Nilesh Shah, 8 years ago

Has patch: set

comment:4 by Sagar Nilesh Shah, 8 years ago

Version: 1.9master

comment:5 by Tim Graham, 8 years ago

Patch needs improvement: set

Comments for improvement on the PR (and some failing tests to be fixed).

comment:6 by Tobias Kunze, 6 years ago

Owner: changed from Sagar Nilesh Shah to Tobias Kunze
Patch needs improvement: unset

comment:7 by Mariusz Felisiak, 6 years ago

Patch needs improvement: set

comment:8 by Mariusz Felisiak, 4 years ago

The proposed solution has some side-effects. We should reach a wider audience and consensus on DevelopersMailingList before moving this forward.

comment:9 by Claude Paroz, 4 years ago

#25594 might be related.

comment:10 by Mariusz Felisiak, 21 months ago

Type: BugNew feature

comment:11 by Ülgen Sarıkavak, 8 months ago

Cc: Ülgen Sarıkavak added
Note: See TracTickets for help on using tickets.
Back to Top