Commits

James Crasta  committed 4abd956

Allow overriding the 'for=' on Label

  • Participants
  • Parent commits ef4353b

Comments (0)

Files changed (2)

File tests/fields.py

         t2 = TextField('').bind(Form(), 'foo_bar')
         self.assertEqual(t2.label.text, '')
 
+    def test_override_for(self):
+        label = Label('test', 'Caption')
+        self.assertEqual(label(for_='foo'), """<label for="foo">Caption</label>""")
+        self.assertEqual(label(**{'for':'bar'}), """<label for="bar">Caption</label>""")
+
+
 
 class FlagsTest(TestCase):
     def setUp(self):

File wtforms/fields/core.py

         return self()
 
     def __call__(self, text=None, **kwargs):
-        kwargs['for'] = self.field_id
+        if 'for_' in kwargs:
+            kwargs['for'] = kwargs.pop('for_')
+        else:
+            kwargs.setdefault('for', self.field_id)
+
         attributes = widgets.html_params(**kwargs)
         return widgets.HTMLString('<label %s>%s</label>' % (attributes, text or self.text))