Opened 18 years ago

Closed 18 years ago

Last modified 18 years ago

#2760 closed defect (fixed)

[patch] Admin interface doesn't handle negative FloatFields

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

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 James Bennett 18 years ago.
Patch which hopefully fixes this
validators.2.diff (1.1 KB ) - added by Eddy Mulyono <eddymulyono@…> 18 years ago.
This works for me.

Download all attachments as: .zip

Change History (9)

by James Bennett, 18 years ago

Attachment: validators.diff added

Patch which hopefully fixes this

comment:1 by James Bennett, 18 years ago

Summary: Admin interface doesn't handle negative FloatFields[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 by Eddy Mulyono <eddymulyono@…>, 18 years ago

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 by Malcolm Tredinnick, 18 years ago

Resolution: fixed
Status: newclosed

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

comment:4 by Eddy Mulyono <eddymulyono@…>, 18 years ago

Resolution: fixed
Status: closedreopened

bug still present in r3843.

by Eddy Mulyono <eddymulyono@…>, 18 years ago

Attachment: validators.2.diff added

This works for me.

comment:5 by Malcolm Tredinnick, 18 years ago

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 by Malcolm Tredinnick, 18 years ago

Resolution: fixed
Status: reopenedclosed

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

comment:7 by Malcolm Tredinnick, 18 years ago

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