﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
4284	forms containing MultiWidgets cannot be reconstructed with clean_data	jfindlay@…	nobody	"This seems to be a design issue that may not be easy to resolve, but there is no easy way to reconstruct a MultiValueField with its own clean data.  Whereas a normal field will accept data from clean_data, a MultiValueField expects it to be 'decompressed' first.
{{{
>>> from datetime import *
>>> from django.newforms import *
>>> 
>>> class MonthYearWidget(MultiWidget):
...   def __init__(self,months=(),years=(),attrs=None):
...     widgets = (Select(choices=months),Select(choices=years))
...     super(MonthYearWidget,self).__init__(widgets,attrs)
...   def decompress(self,value):
...     if value:
...       return value.split('/')
...     return ['', '']
... 
>>> class MonthYearField(MultiValueField):
...   def __init__(self,months=(),years=(),required=True,widget=None,label=None,initial=None):
...     fields = (ChoiceField(choices=months),ChoiceField(choices=years))
...     widget = MonthYearWidget(months=months,years=years)
...     super(MonthYearField,self).__init__(fields,required,widget,label,initial)
...   def compress(self,data_list):
...     if data_list:
...       return '/'.join(data_list)
...     return None
... 
>>> month_list = [x + 1 for x in xrange(12)]
>>> months = [(m,m) for m in month_list]
>>> year_list = [(date.today() + timedelta(days=366)*x).strftime(""%Y"") for x in xrange(10)]
>>> years = [(y,y) for y in year_list]
>>> 
>>> class PaymentForm(Form):
...   ccn = RegexField('^\d{4}[ .-]?\d{4}[ .-]?\d{4}[ .-]?\d{4}$',widget=TextInput(attrs={'maxlength':19,'size':19}))
...   exp_date = MonthYearField(months=months,years=years)
... 
>>> POST = {'ccn':'1234 5678 9012 3456','exp_date_0':'5','exp_date_1':'2007'}
>>> pf = PaymentForm(POST)
>>> pf.is_valid()
True
>>> pf.clean_data
{'ccn': u'1234 5678 9012 3456', 'exp_date': u'5/2007'}
>>> pf1 = PaymentForm(pf.clean_data)
>>> pf1.is_valid()
False
>>> pf1.errors
{'exp_date': [u'This field is required.']}
}}}
"		closed	Forms	dev		wontfix	MultiValueField MultiWidget clean_data decompress		Unreviewed	0	0	0	0	0	0
