Django

Code

Changeset 4136

Show
Ignore:
Timestamp:
11/29/06 11:00:34 (2 years ago)
Author:
adrian
Message:

newforms: Added Widget.value_from_datadict hook, which allows a Widget to define how to convert its post data dictionary to a value. Implemented it for CheckboxSelectMultiple? and updated unit tests

Files:

Legend:

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

    r4133 r4136  
    119119            return 
    120120        for name, field in self.fields.items(): 
    121             value = self.data.get(name, None) 
     121            # value_from_datadict() gets the data from the dictionary. 
     122            # Each widget type knows how to retrieve its own data, because some 
     123            # widgets split data over several HTML fields. 
     124            value = field.widget.value_from_datadict(self.data, name) 
    122125            try: 
    123126                value = field.clean(value) 
  • django/trunk/django/newforms/widgets.py

    r4134 r4136  
    3636            attrs.update(extra_attrs) 
    3737        return attrs 
     38 
     39    def value_from_datadict(self, data, name): 
     40        """ 
     41        Given a dictionary of data and this widget's name, returns the value 
     42        of this widget. Returns None if it's not provided. 
     43        """ 
     44        return data.get(name, None) 
    3845 
    3946    def id_for_label(self, id_): 
     
    187194        for option_value, option_label in chain(self.choices, choices): 
    188195            option_value = smart_unicode(option_value) 
    189             field_name = unicode(name + option_value) 
     196            field_name = name + option_value 
    190197            rendered_cb = cb.render(field_name, (option_value in str_values)) 
    191198            output.append(u'<li><label>%s %s</label></li>' % (rendered_cb, escape(smart_unicode(option_label)))) 
    192199        output.append(u'</ul>') 
    193200        return u'\n'.join(output) 
     201 
     202    def value_from_datadict(self, data, name): 
     203        data_list = [k for k, v in self.choices if data.get(name + k)] 
     204        return data_list or None 
    194205 
    195206    def id_for_label(self, id_): 
  • django/trunk/tests/regressiontests/forms/tests.py

    r4132 r4136  
    14721472</select> 
    14731473 
     1474Add widget=RadioSelect to use that widget with a ChoiceField. 
    14741475>>> class FrameworkForm(Form): 
    14751476...     name = CharField() 
     
    15451546<option value="P" selected="selected">Paul McCartney</option> 
    15461547</select> 
     1548 
     1549MultipleChoiceField can also be used with the CheckboxSelectMultiple widget. 
     1550>>> class SongForm(Form): 
     1551...     name = CharField() 
     1552...     composers = MultipleChoiceField(choices=[('J', 'John Lennon'), ('P', 'Paul McCartney')], widget=CheckboxSelectMultiple) 
     1553>>> f = SongForm() 
     1554>>> print f['composers'] 
     1555<ul> 
     1556<li><label><input type="checkbox" name="composersJ" /> John Lennon</label></li> 
     1557<li><label><input type="checkbox" name="composersP" /> Paul McCartney</label></li> 
     1558</ul> 
     1559>>> f = SongForm({'composers': ['J']}) 
     1560>>> print f['composers'] 
     1561<ul> 
     1562<li><label><input checked="checked" type="checkbox" name="composersJ" /> John Lennon</label></li> 
     1563<li><label><input type="checkbox" name="composersP" /> Paul McCartney</label></li> 
     1564</ul> 
     1565>>> f = SongForm({'composers': ['J', 'P']}) 
     1566>>> print f['composers'] 
     1567<ul> 
     1568<li><label><input checked="checked" type="checkbox" name="composersJ" /> John Lennon</label></li> 
     1569<li><label><input checked="checked" type="checkbox" name="composersP" /> Paul McCartney</label></li> 
     1570</ul> 
     1571 
     1572When using CheckboxSelectMultiple, the framework automatically converts the 
     1573data in clean_data to a list of values, rather than the underlying HTML form 
     1574field name. 
     1575>>> f = SongForm({'name': 'Yesterday'}) 
     1576>>> f.errors 
     1577{'composers': [u'This field is required.']} 
     1578>>> f = SongForm({'name': 'Yesterday', 'composersJ': 'on'}) 
     1579>>> f.errors 
     1580{} 
     1581>>> f.clean_data 
     1582{'composers': [u'J'], 'name': u'Yesterday'} 
     1583>>> f = SongForm({'name': 'Yesterday', 'composersJ': 'on', 'composersP': 'on'}) 
     1584>>> f.errors 
     1585{} 
     1586>>> f.clean_data 
     1587{'composers': [u'J', u'P'], 'name': u'Yesterday'} 
    15471588 
    15481589There are a couple of ways to do multiple-field validation. If you want the