Opened 9 years ago

Closed 9 years ago

Last modified 9 years ago

#2760 closed defect (fixed)

[patch] Admin interface doesn't handle negative FloatFields

Reported by: Eddy Mulyono <eddymulyono@…> Owned by: adrian
Component: contrib.admin Version: master
Severity: normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

  1. Create a model with
    FloatField(max_digits=8, decimal_places=5, blank=True)
    
  2. Use the Admin interface to set the FloatField to '-122.19217'

Expected:
Changes saved.

Bug:

  • "Please correct the error below."
  • "Please enter a valid decimal number with a whole part of at most 3 digits."
  • Entry truncated to '-122.1921', because the HTML input can only take 9 characters (max_digits=8 plus the decimal separator '.')

Attachments (2)

validators.diff (1.5 KB) - added by ubernostrum 9 years ago.
Patch which hopefully fixes this
validators.2.diff (1.1 KB) - added by Eddy Mulyono <eddymulyono@…> 9 years ago.
This works for me.

Download all attachments as: .zip

Change History (9)

Changed 9 years ago by ubernostrum

Patch which hopefully fixes this

comment:1 Changed 9 years ago by ubernostrum

  • Summary changed from Admin interface doesn't handle negative FloatFields to [patch] Admin interface doesn't handle negative FloatFields

I've attached a patch which changes the IsValidFloat validator to allow an extra character in the float if it starts with a '-'; I'm not 100% certain, but I think this fixes the bug.

comment:2 Changed 9 years ago by Eddy Mulyono <eddymulyono@…>

Thanx for the speedy patch. Patch didn't fix the issue, because the HTML <input> still only allows 9 characters, so len(data) POSTed to the view still is 9 chars long, so we never enter the negative-sign-handling code.

if len(data) > (self.max_digits + 1):
    max_allowed_length = data.startswith('-') and (self.max_digits + 2) or ...

We probably need to change the template/widget to have <input size="{{ max_digits + 2 }}">.

comment:3 Changed 9 years ago by mtredinnick

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

(In [3788]) Fixed #2760 -- Fixed validation of negative float amounts in forms. Thanks,
James Bennett and Eddy Mulyono.

comment:4 Changed 9 years ago by Eddy Mulyono <eddymulyono@…>

  • Resolution fixed deleted
  • Status changed from closed to reopened

bug still present in r3843.

Changed 9 years ago by Eddy Mulyono <eddymulyono@…>

This works for me.

comment:5 Changed 9 years ago by mtredinnick

The asymmetry (both in the original patch and in this revised one) bothers me. It looks like we should be using max_allowed_length or self.max_digits consistently.

I'll test this out and commit a patch shortly. Sorry about screwing it up the first time; I didn't check it with my brain switched on.

comment:6 Changed 9 years ago by mtredinnick

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

(In [3845]) Fixed #2760 -- Fixed FloatField validator right (take two!) for negative
float amounts.

comment:7 Changed 9 years ago by mtredinnick

So r3845 seems to allow all legal combinations and rejects -1234, -123.456789, 1234, and 123.456789. I think that's got all the possibilities.

If I have missed a case, can you post the float that fails when you reopen, please.

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