Ticket #6873: SelectMultiple-default-size-00.diff
File SelectMultiple-default-size-00.diff, 4.3 KB (added by , 17 years ago) |
---|
-
django/newforms/widgets.py
375 375 def render(self, name, value, attrs=None, choices=()): 376 376 if value is None: value = [] 377 377 final_attrs = self.build_attrs(attrs, name=name) 378 output = [u'<select multiple="multiple"%s>' % flatatt(final_attrs)] 378 output = [] 379 choice_count = 0 379 380 str_values = set([force_unicode(v) for v in value]) # Normalize to strings. 380 381 for option_value, option_label in chain(self.choices, choices): 382 choice_count += 1 381 383 option_value = force_unicode(option_value) 382 384 selected_html = (option_value in str_values) and ' selected="selected"' or '' 383 385 output.append(u'<option value="%s"%s>%s</option>' % ( 384 386 escape(option_value), selected_html, 385 387 conditional_escape(force_unicode(option_label)))) 388 # default size attribute for cross-browser consistent rendering 389 if 'size' not in final_attrs: 390 # emulate the behaviour of firefox: adjust the size to the number of choices, with an upper boundary 391 final_attrs['size'] = min(10, choice_count) 392 # we have to prepend because choices can be any iterable and we cannot call len() so we have to count them first 393 output = [u'<select multiple="multiple"%s>' % flatatt(final_attrs)] + output 386 394 output.append(u'</select>') 387 395 return mark_safe(u'\n'.join(output)) 388 396 -
tests/regressiontests/forms/extra.py
259 259 >>> w = ComplexMultiWidget() 260 260 >>> print w.render('name', 'some text,JP,2007-04-25 06:24:00') 261 261 <input type="text" name="name_0" value="some text" /> 262 <select multiple="multiple" name="name_1" >262 <select multiple="multiple" name="name_1" size="4"> 263 263 <option value="J" selected="selected">John</option> 264 264 <option value="P" selected="selected">Paul</option> 265 265 <option value="G">George</option> … … 300 300 >>> f = ComplexFieldForm() 301 301 >>> print f 302 302 <tr><th><label for="id_field1_0">Field1:</label></th><td><input type="text" name="field1_0" id="id_field1_0" /> 303 <select multiple="multiple" name="field1_1" id="id_field1_1">303 <select multiple="multiple" size="4" name="field1_1" id="id_field1_1"> 304 304 <option value="J">John</option> 305 305 <option value="P">Paul</option> 306 306 <option value="G">George</option> … … 311 311 >>> f = ComplexFieldForm({'field1_0':'some text','field1_1':['J','P'], 'field1_2_0':'2007-04-25', 'field1_2_1':'06:24:00'}) 312 312 >>> print f 313 313 <tr><th><label for="id_field1_0">Field1:</label></th><td><input type="text" name="field1_0" value="some text" id="id_field1_0" /> 314 <select multiple="multiple" name="field1_1" id="id_field1_1">314 <select multiple="multiple" size="4" name="field1_1" id="id_field1_1"> 315 315 <option value="J" selected="selected">John</option> 316 316 <option value="P" selected="selected">Paul</option> 317 317 <option value="G">George</option> -
tests/regressiontests/forms/forms.py
475 475 ... composers = MultipleChoiceField() 476 476 >>> f = SongForm(auto_id=False) 477 477 >>> print f['composers'] 478 <select multiple="multiple" name="composers" >478 <select multiple="multiple" name="composers" size="0"> 479 479 </select> 480 480 >>> class SongForm(Form): 481 481 ... name = CharField() 482 482 ... composers = MultipleChoiceField(choices=[('J', 'John Lennon'), ('P', 'Paul McCartney')]) 483 483 >>> f = SongForm(auto_id=False) 484 484 >>> print f['composers'] 485 <select multiple="multiple" name="composers" >485 <select multiple="multiple" name="composers" size="2"> 486 486 <option value="J">John Lennon</option> 487 487 <option value="P">Paul McCartney</option> 488 488 </select> … … 490 490 >>> print f['name'] 491 491 <input type="text" name="name" value="Yesterday" /> 492 492 >>> print f['composers'] 493 <select multiple="multiple" name="composers" >493 <select multiple="multiple" name="composers" size="2"> 494 494 <option value="J">John Lennon</option> 495 495 <option value="P" selected="selected">Paul McCartney</option> 496 496 </select>