Changeset 4136
- Timestamp:
- 11/29/06 11:00:34 (2 years ago)
- Files:
-
- django/trunk/django/newforms/forms.py (modified) (1 diff)
- 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/forms.py
r4133 r4136 119 119 return 120 120 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) 122 125 try: 123 126 value = field.clean(value) django/trunk/django/newforms/widgets.py
r4134 r4136 36 36 attrs.update(extra_attrs) 37 37 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) 38 45 39 46 def id_for_label(self, id_): … … 187 194 for option_value, option_label in chain(self.choices, choices): 188 195 option_value = smart_unicode(option_value) 189 field_name = unicode(name + option_value)196 field_name = name + option_value 190 197 rendered_cb = cb.render(field_name, (option_value in str_values)) 191 198 output.append(u'<li><label>%s %s</label></li>' % (rendered_cb, escape(smart_unicode(option_label)))) 192 199 output.append(u'</ul>') 193 200 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 194 205 195 206 def id_for_label(self, id_): django/trunk/tests/regressiontests/forms/tests.py
r4132 r4136 1472 1472 </select> 1473 1473 1474 Add widget=RadioSelect to use that widget with a ChoiceField. 1474 1475 >>> class FrameworkForm(Form): 1475 1476 ... name = CharField() … … 1545 1546 <option value="P" selected="selected">Paul McCartney</option> 1546 1547 </select> 1548 1549 MultipleChoiceField 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 1572 When using CheckboxSelectMultiple, the framework automatically converts the 1573 data in clean_data to a list of values, rather than the underlying HTML form 1574 field 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'} 1547 1588 1548 1589 There are a couple of ways to do multiple-field validation. If you want the
