Commits

James Crasta committed 3db7109

Add step="any" to HTML5 DecimalField and step=1 to IntegerField. Fixes #165.

Comments (0)

Files changed (3)

 from unittest import TestCase
 from wtforms.widgets import html_params, Input
 from wtforms.widgets import *
-
+from wtforms.widgets import html5
 
 class DummyField(object):
     def __init__(self, data, name='f', label='', id='', type='TextField'):
 class BasicWidgetsTest(TestCase):
     """Test most of the basic input widget types"""
 
-    field = DummyField('foo', name='bar', label='label', id='id') 
+    field = DummyField('foo', name='bar', label='label', id='id')
 
     def test_input_type(self):
         a = Input()
     field = DummyField([('foo', 'lfoo', True), ('bar', 'lbar', False)])
 
     def test(self):
-        self.assertEqual(Select()(self.field), 
+        self.assertEqual(Select()(self.field),
             '<select id="" name="f"><option selected value="foo">lfoo</option><option value="bar">lbar</option></select>')
-        self.assertEqual(Select(multiple=True)(self.field), 
+        self.assertEqual(Select(multiple=True)(self.field),
             '<select id="" multiple name="f"><option selected value="foo">lfoo</option><option value="bar">lbar</option></select>')
 
+
+class HTML5Test(TestCase):
+    field = DummyField('42', name='bar', id='id')
+
+    def test_number(self):
+        i1 = html5.NumberInput(step='any')
+        self.assertEqual(i1(self.field), '<input id="id" name="bar" step="any" type="number" value="42">')
+        i2 = html5.NumberInput(step=2)
+        self.assertEqual(i1(self.field, step=3), '<input id="id" name="bar" step="3" type="number" value="42">')
+
+    def test_number(self):
+        i1 = html5.RangeInput(step='any')
+        self.assertEqual(i1(self.field), '<input id="id" name="bar" step="any" type="range" value="42">')
+        i2 = html5.RangeInput(step=2)
+        self.assertEqual(i1(self.field, step=3), '<input id="id" name="bar" step="3" type="range" value="42">')
+
+
 if __name__ == '__main__':
     from unittest import main
     main()

wtforms/fields/html5.py

     """
     Represents an ``<input type="number">``.
     """
-    widget = widgets.NumberInput()
+    widget = widgets.NumberInput(step='1')
 
 
 class DecimalField(core.DecimalField):
     """
     Represents an ``<input type="number">``.
     """
-    widget = widgets.NumberInput()
+    widget = widgets.NumberInput(step='any')
 
 
 class IntegerRangeField(core.IntegerField):
     """
     Represents an ``<input type="range">``.
     """
-    widget = widgets.RangeInput()
+    widget = widgets.RangeInput(step='1')
 
 
 class DecimalRangeField(core.DecimalField):
     """
     Represents an ``<input type="range">``.
     """
-    widget = widgets.RangeInput()
+    widget = widgets.RangeInput(step='any')
 

wtforms/widgets/html5.py

     """
     input_type = 'number'
 
+    def __init__(self, step=None):
+        self.step = step
+
+    def __call__(self, field, **kwargs):
+        if self.step is not None:
+            kwargs.setdefault('step', self.step)
+        return super(NumberInput, self).__call__(field, **kwargs)
+
 
 class RangeInput(Input):
     """
     """
     input_type = 'range'
 
+    def __init__(self, step=None):
+        self.step = step
+
+    def __call__(self, field, **kwargs):
+        if self.step is not None:
+            kwargs.setdefault('step', self.step)
+        return super(RangeInput, self).__call__(field, **kwargs)
+
 
 class ColorInput(Input):
     """