Changeset 4298
- Timestamp:
- 01/08/07 23:12:25 (2 years ago)
- Files:
-
- django/trunk/django/newforms/fields.py (modified) (3 diffs)
- django/trunk/django/newforms/forms.py (modified) (2 diffs)
- django/trunk/django/newforms/widgets.py (modified) (2 diffs)
- django/trunk/tests/regressiontests/forms/tests.py (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/trunk/django/newforms/fields.py
r4265 r4298 5 5 from django.utils.translation import gettext 6 6 from util import ValidationError, smart_unicode 7 from widgets import TextInput, PasswordInput, CheckboxInput, Select, SelectMultiple7 from widgets import TextInput, PasswordInput, HiddenInput, MultipleHiddenInput, CheckboxInput, Select, SelectMultiple 8 8 import datetime 9 9 import re … … 30 30 class Field(object): 31 31 widget = TextInput # Default widget to use when rendering this type of Field. 32 hidden_widget = HiddenInput # Default widget to use when rendering this as "hidden". 32 33 33 34 # Tracks each time a Field instance is created. Used to retain order. … … 337 338 338 339 class MultipleChoiceField(ChoiceField): 340 hidden_widget = MultipleHiddenInput 341 339 342 def __init__(self, choices=(), required=True, widget=SelectMultiple, label=None, initial=None): 340 343 ChoiceField.__init__(self, choices, required, widget, label, initial) django/trunk/django/newforms/forms.py
r4297 r4298 6 6 from django.utils.html import escape 7 7 from fields import Field 8 from widgets import TextInput, Textarea, HiddenInput 8 from widgets import TextInput, Textarea, HiddenInput, MultipleHiddenInput 9 9 from util import StrAndUnicode, ErrorDict, ErrorList, ValidationError 10 10 … … 239 239 Returns a string of HTML for representing this as an <input type="hidden">. 240 240 """ 241 return self.as_widget( HiddenInput(), attrs)241 return self.as_widget(self.field.hidden_widget(), attrs) 242 242 243 243 def _data(self): django/trunk/django/newforms/widgets.py
r4265 r4298 4 4 5 5 __all__ = ( 6 'Widget', 'TextInput', 'PasswordInput', 'HiddenInput', ' FileInput',7 ' Textarea', 'CheckboxInput',6 'Widget', 'TextInput', 'PasswordInput', 'HiddenInput', 'MultipleHiddenInput', 7 'FileInput', 'Textarea', 'CheckboxInput', 8 8 'Select', 'SelectMultiple', 'RadioSelect', 'CheckboxSelectMultiple', 9 9 ) … … 87 87 input_type = 'hidden' 88 88 is_hidden = True 89 90 class MultipleHiddenInput(HiddenInput): 91 """ 92 A widget that handles <input type="hidden"> for fields that have a list 93 of values. 94 """ 95 def render(self, name, value, attrs=None): 96 if value is None: value = [] 97 final_attrs = self.build_attrs(attrs, type=self.input_type, name=name) 98 return u'\n'.join([(u'<input%s />' % flatatt(dict(value=smart_unicode(v), **final_attrs))) for v in value]) 89 99 90 100 class FileInput(Input): django/trunk/tests/regressiontests/forms/tests.py
r4297 r4298 1850 1850 </select> 1851 1851 1852 MultipleChoiceField rendered as_hidden() is a special case. Because it can 1853 have multiple values, its as_hidden() renders multiple <input type="hidden"> 1854 tags. 1855 >>> f = SongForm({'name': 'Yesterday', 'composers': ['P']}, auto_id=False) 1856 >>> print f['composers'].as_hidden() 1857 <input type="hidden" name="composers" value="P" /> 1858 >>> f = SongForm({'name': 'From Me To You', 'composers': ['P', 'J']}, auto_id=False) 1859 >>> print f['composers'].as_hidden() 1860 <input type="hidden" name="composers" value="P" /> 1861 <input type="hidden" name="composers" value="J" /> 1862 1852 1863 MultipleChoiceField can also be used with the CheckboxSelectMultiple widget. 1853 1864 >>> class SongForm(Form): … … 1905 1916 >>> f.clean_data 1906 1917 {'composers': [u'J', u'P'], 'name': u'Yesterday'} 1918 1919 # Validating multiple fields in relation to another ########################### 1907 1920 1908 1921 There are a couple of ways to do multiple-field validation. If you want the
