Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed #21752 -- Prevented custom widget step attribute to be overwritten

Thanks orcein at gmail.com for the report.
  • Loading branch information...
commit 18374c1d8c37dd251dae72470b6c532bddf140eb 1 parent f7c2c0a
@claudep claudep authored
View
4 django/forms/fields.py
@@ -289,7 +289,7 @@ def validate(self, value):
def widget_attrs(self, widget):
attrs = super(FloatField, self).widget_attrs(widget)
- if isinstance(widget, NumberInput):
+ if isinstance(widget, NumberInput) and 'step' not in widget.attrs:
attrs.setdefault('step', 'any')
return attrs
@@ -377,7 +377,7 @@ def validate(self, value):
def widget_attrs(self, widget):
attrs = super(DecimalField, self).widget_attrs(widget)
- if isinstance(widget, NumberInput):
+ if isinstance(widget, NumberInput) and 'step' not in widget.attrs:
if self.decimal_places is not None:
# Use exponential notation for small values since they might
# be parsed as 0 otherwise. ref #20765
View
6 tests/forms_tests/tests/test_fields.py
@@ -282,6 +282,10 @@ def test_floatfield_3(self):
self.assertEqual(f.max_value, 1.5)
self.assertEqual(f.min_value, 0.5)
+ def test_floatfield_widget_attrs(self):
+ f = FloatField(widget=NumberInput(attrs={'step': 0.01, 'max': 1.0, 'min': 0.0}))
+ self.assertWidgetRendersTo(f, '<input step="0.01" name="f" min="0.0" max="1.0" type="number" id="id_f" />')
+
def test_floatfield_localized(self):
"""
Make sure localized FloatField's widget renders to a text input with
@@ -392,6 +396,8 @@ def test_decimalfield_widget_attrs(self):
self.assertEqual(f.widget_attrs(NumberInput()), {'step': '1e-19'})
f = DecimalField(max_digits=20)
self.assertEqual(f.widget_attrs(NumberInput()), {'step': 'any'})
+ f = DecimalField(max_digits=6, widget=NumberInput(attrs={'step': '0.01'}))
+ self.assertWidgetRendersTo(f, '<input step="0.01" name="f" type="number" id="id_f" />')
def test_decimalfield_localized(self):
"""
Please sign in to comment.
Something went wrong with that request. Please try again.