klipstein/dojango — Issue #49: 49 - NumberSpinnerInput form 
Author: klipstein
Created: 2011-11-06 12:59:54+00:00
State: open

I could not see a form field which would use NumberSpinner, I had to use Select, the following field would be able to use NumberSpinner, instead of Select, when it is integerfield, would this be useful to include?

class NumberChoiceField(forms.IntegerField):
widget = forms.NumberSpinnerInput

def __init__(self, *Args, **keywords):
self._delta = 0
self._max = 0
self._min = 0

if "smallDelta" in keywords:
try:
self._delta = int(keywords['smallDelta'])
except ValueError, message:
raise util.ValidationError, message
finally:
del keywords['smallDelta']

if self._delta <= 0:
raise util.ValidationError, "delta value cannot be <= zero"

if "max_value" in keywords:
try:
self._max = int(keywords["max_value"])
except ValueError, message:
raise util.ValidationError, message

if "min_value" in keywords:
try:
self._min = int(keywords["min_value"])
except ValueError, message:
raise util.ValidationError, message

super(NumberChoiceField, self).__init__(*Args, **keywords)

def widget_attrs(self, widget):
retval = {}
retval['smallDelta'] = self._delta
retval['constraints'] = { 'min': self._min, 'max': self._max, 'places': 0 }
return retval

Original link: http://code.google.com/p/dojango/issues/detail?id=49