Code

Ticket #14710: 14710_FormsCssClassRenderedInBoundField.diff

File 14710_FormsCssClassRenderedInBoundField.diff, 5.5 KB (added by trebor74hr, 3 years ago)

Patch to forms css class rendered for BoundField

Line 
1Index: django/forms/forms.py
2===================================================================
3--- django/forms/forms.py       (revision 14587)
4+++ django/forms/forms.py       (working copy)
5@@ -151,6 +151,7 @@
6             else:
7                 # Create a 'class="..."' atribute if the row should have any
8                 # CSS classes applied.
9+                # Added to container DOM element (e.g. <li>)
10                 css_classes = bf.css_classes()
11                 if css_classes:
12                     html_class_attr = ' class="%s"' % css_classes
13@@ -432,6 +433,13 @@
14             else:
15                 attrs['id'] = self.html_initial_id
16 
17+        # Create a 'class="..."' atribute if the row should have any
18+        # CSS classes applied.
19+        # Added to entry DOM element (e.g. <input>)
20+        css_classes = self.css_classes(attrs.get("class", None))
21+        if css_classes:
22+            attrs["class"]=css_classes
23+
24         if not self.form.is_bound:
25             data = self.form.initial.get(self.name, self.field.initial)
26             if callable(data):
27Index: tests/regressiontests/forms/tests/forms.py
28===================================================================
29--- tests/regressiontests/forms/tests/forms.py  (revision 14587)
30+++ tests/regressiontests/forms/tests/forms.py  (working copy)
31@@ -1672,35 +1672,39 @@
32         p.error_css_class = 'error'
33         p.required_css_class = 'required'
34 
35-        self.assertEqual(p.as_ul(), """<li class="required error"><ul class="errorlist"><li>This field is required.</li></ul><label for="id_name">Name:</label> <input type="text" name="name" id="id_name" /></li>
36-<li class="required"><label for="id_is_cool">Is cool:</label> <select name="is_cool" id="id_is_cool">
37+<input type="text" name="name" id="id_name" />
38+        self.assertEqual(p.as_ul(), """<li class="required error"><ul class="errorlist"><li>This field is required.</li></ul><label for="id_name">Name:</label> <input class="required error" type="text" name="name" id="id_name" /></li>
39+<li class="required"><label for="id_is_cool">Is cool:</label> <select class="required" name="is_cool" id="id_is_cool">
40 <option value="1" selected="selected">Unknown</option>
41 <option value="2">Yes</option>
42 <option value="3">No</option>
43 </select></li>
44 <li><label for="id_email">Email:</label> <input type="text" name="email" id="id_email" /></li>
45-<li class="required error"><ul class="errorlist"><li>This field is required.</li></ul><label for="id_age">Age:</label> <input type="text" name="age" id="id_age" /></li>""")
46+<li class="required error"><ul class="errorlist"><li>This field is required.</li></ul><label for="id_age">Age:</label> <input class="required error" type="text" name="age" id="id_age" /></li>""")
47 
48         self.assertEqual(p.as_p(), """<ul class="errorlist"><li>This field is required.</li></ul>
49-<p class="required error"><label for="id_name">Name:</label> <input type="text" name="name" id="id_name" /></p>
50-<p class="required"><label for="id_is_cool">Is cool:</label> <select name="is_cool" id="id_is_cool">
51+<p class="required error"><label for="id_name">Name:</label> <input class="required error" type="text" name="name" id="id_name" /></p>
52+<p class="required"><label for="id_is_cool">Is cool:</label> <select class="required" name="is_cool" id="id_is_cool">
53 <option value="1" selected="selected">Unknown</option>
54 <option value="2">Yes</option>
55 <option value="3">No</option>
56 </select></p>
57 <p><label for="id_email">Email:</label> <input type="text" name="email" id="id_email" /></p>
58 <ul class="errorlist"><li>This field is required.</li></ul>
59-<p class="required error"><label for="id_age">Age:</label> <input type="text" name="age" id="id_age" /></p>""")
60+<p class="required error"><label for="id_age">Age:</label> <input class="required error" type="text" name="age" id="id_age" /></p>""")
61 
62-        self.assertEqual(p.as_table(), """<tr class="required error"><th><label for="id_name">Name:</label></th><td><ul class="errorlist"><li>This field is required.</li></ul><input type="text" name="name" id="id_name" /></td></tr>
63-<tr class="required"><th><label for="id_is_cool">Is cool:</label></th><td><select name="is_cool" id="id_is_cool">
64+        self.assertEqual(p.as_table(), """<tr class="required error"><th><label for="id_name">Name:</label></th><td><ul class="errorlist"><li>This field is required.</li></ul><input class="required error" type="text" name="name" id="id_name" /></td></tr>
65+<tr class="required"><th><label for="id_is_cool">Is cool:</label></th><td><select class="required" name="is_cool" id="id_is_cool">
66 <option value="1" selected="selected">Unknown</option>
67 <option value="2">Yes</option>
68 <option value="3">No</option>
69 </select></td></tr>
70 <tr><th><label for="id_email">Email:</label></th><td><input type="text" name="email" id="id_email" /></td></tr>
71-<tr class="required error"><th><label for="id_age">Age:</label></th><td><ul class="errorlist"><li>This field is required.</li></ul><input type="text" name="age" id="id_age" /></td></tr>""")
72+<tr class="required error"><th><label for="id_age">Age:</label></th><td><ul class="errorlist"><li>This field is required.</li></ul><input class="required error" type="text" name="age" id="id_age" /></td></tr>""")
73 
74+        # render one bound field - case http://docs.djangoproject.com/en/dev/topics/forms/#looping-over-the-form-s-fields
75+        self.assertEqual(unicode(p["name"]), '<input class="required error" type="text" name="name" id="id_name" />')
76+
77     def test_label_split_datetime_not_displayed(self):
78         class EventForm(Form):
79             happened_at = SplitDateTimeField(widget=widgets.SplitHiddenDateTimeWidget)