Django

Code

Changeset 4298

Show
Ignore:
Timestamp:
01/08/07 23:12:25 (2 years ago)
Author:
adrian
Message:

Fixed #3193 -- newforms: Modified as_hidden() to handle MultipleChoiceField? correctly. Thanks for the report, Honza Kral

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/newforms/fields.py

    r4265 r4298  
    55from django.utils.translation import gettext 
    66from util import ValidationError, smart_unicode 
    7 from widgets import TextInput, PasswordInput, CheckboxInput, Select, SelectMultiple 
     7from widgets import TextInput, PasswordInput, HiddenInput, MultipleHiddenInput, CheckboxInput, Select, SelectMultiple 
    88import datetime 
    99import re 
     
    3030class Field(object): 
    3131    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". 
    3233 
    3334    # Tracks each time a Field instance is created. Used to retain order. 
     
    337338 
    338339class MultipleChoiceField(ChoiceField): 
     340    hidden_widget = MultipleHiddenInput 
     341 
    339342    def __init__(self, choices=(), required=True, widget=SelectMultiple, label=None, initial=None): 
    340343        ChoiceField.__init__(self, choices, required, widget, label, initial) 
  • django/trunk/django/newforms/forms.py

    r4297 r4298  
    66from django.utils.html import escape 
    77from fields import Field 
    8 from widgets import TextInput, Textarea, HiddenInput 
     8from widgets import TextInput, Textarea, HiddenInput, MultipleHiddenInput 
    99from util import StrAndUnicode, ErrorDict, ErrorList, ValidationError 
    1010 
     
    239239        Returns a string of HTML for representing this as an <input type="hidden">. 
    240240        """ 
    241         return self.as_widget(HiddenInput(), attrs) 
     241        return self.as_widget(self.field.hidden_widget(), attrs) 
    242242 
    243243    def _data(self): 
  • django/trunk/django/newforms/widgets.py

    r4265 r4298  
    44 
    55__all__ = ( 
    6     'Widget', 'TextInput', 'PasswordInput', 'HiddenInput', 'FileInput', 
    7     'Textarea', 'CheckboxInput', 
     6    'Widget', 'TextInput', 'PasswordInput', 'HiddenInput', 'MultipleHiddenInput', 
     7    'FileInput', 'Textarea', 'CheckboxInput', 
    88    'Select', 'SelectMultiple', 'RadioSelect', 'CheckboxSelectMultiple', 
    99) 
     
    8787    input_type = 'hidden' 
    8888    is_hidden = True 
     89 
     90class 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]) 
    8999 
    90100class FileInput(Input): 
  • django/trunk/tests/regressiontests/forms/tests.py

    r4297 r4298  
    18501850</select> 
    18511851 
     1852MultipleChoiceField rendered as_hidden() is a special case. Because it can 
     1853have multiple values, its as_hidden() renders multiple <input type="hidden"> 
     1854tags. 
     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 
    18521863MultipleChoiceField can also be used with the CheckboxSelectMultiple widget. 
    18531864>>> class SongForm(Form): 
     
    19051916>>> f.clean_data 
    19061917{'composers': [u'J', u'P'], 'name': u'Yesterday'} 
     1918 
     1919# Validating multiple fields in relation to another ########################### 
    19071920 
    19081921There are a couple of ways to do multiple-field validation. If you want the