Ticket #8426: wrap_help_text_in_css_class-with_tests.diff

File wrap_help_text_in_css_class-with_tests.diff, 8.2 KB (added by mrts, 7 years ago)

Add tests and fix as_p()

  • django/forms/forms.py

     
    187187
    188188    def as_table(self):
    189189        "Returns this form rendered as HTML <tr>s -- excluding the <table></table>."
    190         return self._html_output(u'<tr><th>%(label)s</th><td>%(errors)s%(field)s%(help_text)s</td></tr>', u'<tr><td colspan="2">%s</td></tr>', '</td></tr>', u'<br />%s', False)
     190        return self._html_output(u'<tr><th>%(label)s</th><td>%(errors)s%(field)s%(help_text)s</td></tr>', u'<tr><td colspan="2">%s</td></tr>', '</td></tr>', u'<br /><span class="helptext">%s</span>', False)
    191191
    192192    def as_ul(self):
    193193        "Returns this form rendered as HTML <li>s -- excluding the <ul></ul>."
    194         return self._html_output(u'<li>%(errors)s%(label)s %(field)s%(help_text)s</li>', u'<li>%s</li>', '</li>', u' %s', False)
     194        return self._html_output(u'<li>%(errors)s%(label)s %(field)s%(help_text)s</li>', u'<li>%s</li>', '</li>', u' <span class="helptext">%s</span>', False)
    195195
    196196    def as_p(self):
    197197        "Returns this form rendered as HTML <p>s."
    198         return self._html_output(u'<p>%(label)s %(field)s%(help_text)s</p>', u'%s', '</p>', u' %s', True)
     198        return self._html_output(u'<p>%(label)s %(field)s%(help_text)s</p>', u'%s', '</p>', u' <span class="helptext">%s</span>', True)
    199199
    200200    def non_field_errors(self):
    201201        """
  • tests/modeltests/model_forms/models.py

     
    431431<option value="1">Entertainment</option>
    432432<option value="2">It&#39;s a test</option>
    433433<option value="3">Third test</option>
    434 </select><br /> Hold down "Control", or "Command" on a Mac, to select more than one.</td></tr>
     434</select><br /><span class="helptext"> Hold down "Control", or "Command" on a Mac, to select more than one.</span></td></tr>
    435435
    436436You can restrict a form to a subset of the complete list of fields
    437437by providing a 'fields' argument. If you try to save a
     
    456456...         model = Writer
    457457>>> f = RoykoForm(auto_id=False, instance=w)
    458458>>> print f
    459 <tr><th>Name:</th><td><input type="text" name="name" value="Mike Royko" maxlength="50" /><br />Use both first and last names.</td></tr>
     459<tr><th>Name:</th><td><input type="text" name="name" value="Mike Royko" maxlength="50" /><br /><span class="helptext">Use both first and last names.</span></td></tr>
    460460
    461461>>> art = Article(headline='Test article', slug='test-article', pub_date=datetime.date(1988, 1, 4), writer=w, article='Hello.')
    462462>>> art.save()
     
    486486<option value="1">Entertainment</option>
    487487<option value="2">It&#39;s a test</option>
    488488<option value="3">Third test</option>
    489 </select>  Hold down "Control", or "Command" on a Mac, to select more than one.</li>
     489</select> <span class="helptext"> Hold down "Control", or "Command" on a Mac, to select more than one.</span></li>
    490490>>> f = TestArticleForm({'headline': u'Test headline', 'slug': 'test-headline', 'pub_date': u'1984-02-06', 'writer': u'1', 'article': 'Hello.'}, instance=art)
    491491>>> f.is_valid()
    492492True
     
    547547<option value="1" selected="selected">Entertainment</option>
    548548<option value="2">It&#39;s a test</option>
    549549<option value="3">Third test</option>
    550 </select>  Hold down "Control", or "Command" on a Mac, to select more than one.</li>
     550</select> <span class="helptext"> Hold down "Control", or "Command" on a Mac, to select more than one.</span></li>
    551551
    552552>>> f = TestArticleForm({'headline': u'New headline', 'slug': u'new-headline', 'pub_date': u'1988-01-04',
    553553...     'writer': u'1', 'article': u'Hello.', 'categories': [u'1', u'2']}, instance=new_art)
     
    663663<option value="1">Entertainment</option>
    664664<option value="2">It&#39;s a test</option>
    665665<option value="3">Third</option>
    666 </select>  Hold down "Control", or "Command" on a Mac, to select more than one.</li>
     666</select> <span class="helptext"> Hold down "Control", or "Command" on a Mac, to select more than one.</span></li>
    667667>>> Category.objects.create(name='Fourth', url='4th')
    668668<Category: Fourth>
    669669>>> Writer.objects.create(name='Carl Bernstein')
     
    690690<option value="2">It&#39;s a test</option>
    691691<option value="3">Third</option>
    692692<option value="4">Fourth</option>
    693 </select>  Hold down "Control", or "Command" on a Mac, to select more than one.</li>
     693</select> <span class="helptext"> Hold down "Control", or "Command" on a Mac, to select more than one.</span></li>
    694694
    695695# ModelChoiceField ############################################################
    696696
  • tests/regressiontests/forms/forms.py

     
    12051205...    password = CharField(widget=PasswordInput, help_text='Choose wisely.')
    12061206>>> p = UserRegistration(auto_id=False)
    12071207>>> print p.as_ul()
    1208 <li>Username: <input type="text" name="username" maxlength="10" /> e.g., user@example.com</li>
    1209 <li>Password: <input type="password" name="password" /> Choose wisely.</li>
     1208<li>Username: <input type="text" name="username" maxlength="10" /> <span class="helptext">e.g., user@example.com</span></li>
     1209<li>Password: <input type="password" name="password" /> <span class="helptext">Choose wisely.</span></li>
    12101210>>> print p.as_p()
    1211 <p>Username: <input type="text" name="username" maxlength="10" /> e.g., user@example.com</p>
    1212 <p>Password: <input type="password" name="password" /> Choose wisely.</p>
     1211<p>Username: <input type="text" name="username" maxlength="10" /> <span class="helptext">e.g., user@example.com</span></p>
     1212<p>Password: <input type="password" name="password" /> <span class="helptext">Choose wisely.</span></p>
    12131213>>> print p.as_table()
    1214 <tr><th>Username:</th><td><input type="text" name="username" maxlength="10" /><br />e.g., user@example.com</td></tr>
    1215 <tr><th>Password:</th><td><input type="password" name="password" /><br />Choose wisely.</td></tr>
     1214<tr><th>Username:</th><td><input type="text" name="username" maxlength="10" /><br /><span class="helptext">e.g., user@example.com</span></td></tr>
     1215<tr><th>Password:</th><td><input type="password" name="password" /><br /><span class="helptext">Choose wisely.</span></td></tr>
    12161216
    12171217The help text is displayed whether or not data is provided for the form.
    12181218>>> p = UserRegistration({'username': u'foo'}, auto_id=False)
    12191219>>> print p.as_ul()
    1220 <li>Username: <input type="text" name="username" value="foo" maxlength="10" /> e.g., user@example.com</li>
    1221 <li><ul class="errorlist"><li>This field is required.</li></ul>Password: <input type="password" name="password" /> Choose wisely.</li>
     1220<li>Username: <input type="text" name="username" value="foo" maxlength="10" /> <span class="helptext">e.g., user@example.com</span></li>
     1221<li><ul class="errorlist"><li>This field is required.</li></ul>Password: <input type="password" name="password" /> <span class="helptext">Choose wisely.</span></li>
    12221222
    12231223help_text is not displayed for hidden fields. It can be used for documentation
    12241224purposes, though.
     
    12281228...    next = CharField(widget=HiddenInput, initial='/', help_text='Redirect destination')
    12291229>>> p = UserRegistration(auto_id=False)
    12301230>>> print p.as_ul()
    1231 <li>Username: <input type="text" name="username" maxlength="10" /> e.g., user@example.com</li>
     1231<li>Username: <input type="text" name="username" maxlength="10" /> <span class="helptext">e.g., user@example.com</span></li>
    12321232<li>Password: <input type="password" name="password" /><input type="hidden" name="next" value="/" /></li>
    12331233
    12341234Help text can include arbitrary Unicode characters.
     
    12361236...    username = CharField(max_length=10, help_text='ŠĐĆŽćžšđ')
    12371237>>> p = UserRegistration(auto_id=False)
    12381238>>> p.as_ul()
    1239 u'<li>Username: <input type="text" name="username" maxlength="10" /> \u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111</li>'
     1239u'<li>Username: <input type="text" name="username" maxlength="10" /> <span class="helptext">\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111</span></li>'
    12401240
    12411241# Subclassing forms ###########################################################
    12421242
Back to Top