#26834: MinValueValidator/MaxValueValidator not forwarded to form field for ModelForm -------------------------+------------------------------------------------- Reporter: sergei- | Owner: nobody maertens | Type: Bug | Status: new Component: Forms | Version: 1.9 Severity: Normal | Keywords: MaxValueValidator, Triage Stage: | MinValueValidator, ModelForm, IntegerField Unreviewed | Has patch: 0 Easy pickings: 0 | UI/UX: 0 -------------------------+------------------------------------------------- I just ran into a possible enhancement.
Consider the following model: {{{ #!python 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: {{{ #!python 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. -- Ticket URL: <https://code.djangoproject.com/ticket/26834> Django <https://code.djangoproject.com/> The Web framework for perfectionists with deadlines. -- You received this message because you are subscribed to the Google Groups "Django updates" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-updates+unsubscr...@googlegroups.com. To post to this group, send email to django-updates@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/058.1978efbcba6090b6820161e4f7f58e74%40djangoproject.com. For more options, visit https://groups.google.com/d/optout.