Opened 8 years ago

Closed 8 years ago

#4415 closed (fixed)

PositiveIntegerField in models.py does not get validation in form_for_model

Reported by: django@… Owned by: oggie_rob
Component: Forms Version: master
Severity: Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

Negative values do not raise a ValidationError in the form delivered by form_for_model.

One solution would be to create a newforms.PositiveIntegerField class, another would be to just use newforms.IntegerField with a default value of min_value = 0.

Attachments (5)

positiveintegerfield.diff (660 bytes) - added by django@… 8 years ago.
PositiveIntegerField as newforms.IntergerField with min_value
positiveintegerfield2.diff (957 bytes) - added by django@… 8 years ago.
new class: newforms.PositiveIntegerField
positiveintegerfield2.2.diff (945 bytes) - added by Will Hardy <will@…> 8 years ago.
new class: newforms.PositiveIntegerField
4415_fields.diff (1.0 KB) - added by oggie_rob 8 years ago.
formfield method changes to PositiveIntegerField and PositiveSmallIntegerField
4415_tests.diff (973 bytes) - added by oggie_rob 8 years ago.
Test of positive integer values via form_for_model. Used generic "boundary" model which can be extended.

Download all attachments as: .zip

Change History (15)

Changed 8 years ago by django@…

PositiveIntegerField as newforms.IntergerField with min_value

Changed 8 years ago by django@…

new class: newforms.PositiveIntegerField

comment:1 follow-up: Changed 8 years ago by mtredinnick

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

Adding yet another newforms field isn't necessary here. We already have IntegerField, as you note and it can supply all the necessary functionality.

Looks like writing a formfield() method for django.db.models.fields.PostiveIntegerField would be the right solution.

Changed 8 years ago by Will Hardy <will@…>

new class: newforms.PositiveIntegerField

comment:2 in reply to: ↑ description Changed 8 years ago by Will Hardy <will@…>

New attachment added after copy and paste error in the positiveintegerfield2.diff

comment:3 in reply to: ↑ 1 Changed 8 years ago by Will Hardy <will@…>

Replying to mtredinnick:

Adding yet another newforms field isn't necessary here. We already have IntegerField, as you note and it can supply all the necessary functionality.

Sorry, didn't see this comment before fixing the second patch.

Looks like writing a formfield() method for django.db.models.fields.PostiveIntegerField would be the right solution.

Good, then feel free to ignore/delete positiveintegerfield2.diff and see positiveintegerfield.diff for this change.

comment:4 Changed 8 years ago by django@…

Also, if someone could replace my email address above with django@…, that would be nice.

comment:5 Changed 8 years ago by anonymous

  • Owner changed from nobody to anonymous
  • Status changed from new to assigned

comment:6 Changed 8 years ago by oggie_rob

  • Owner changed from anonymous to oggie_rob
  • Status changed from assigned to new

Changed 8 years ago by oggie_rob

formfield method changes to PositiveIntegerField and PositiveSmallIntegerField

Changed 8 years ago by oggie_rob

Test of positive integer values via form_for_model. Used generic "boundary" model which can be extended.

comment:7 Changed 8 years ago by oggie_rob

  • Status changed from new to assigned

Didn't need to create a new form field, just modified the formfield() method.
Added tests for boundary validation only via form_for_model method.

comment:8 Changed 8 years ago by oggie_rob

The last two patches (named 4415_*) are the only ones you need.

comment:9 Changed 8 years ago by SmileyChris

  • Triage Stage changed from Unreviewed to Ready for checkin

Normal method is to just have one single patch, but it looks good oggie_rob.

comment:10 Changed 8 years ago by russellm

  • Resolution set to fixed
  • Status changed from assigned to closed

(In [6252]) Fixed #4415 -- Added formfield method to PositiveIntegerField, so that those fields get validated correctly. Thanks, Oggie Rob.

Note: See TracTickets for help on using tickets.
Back to Top