#4284: forms containing MultiWidgets cannot be reconstructed with clean_data
-------------------------------+--------------------------------------------
Reporter:  [EMAIL PROTECTED]  |       Owner:  adrian                            
               
  Status:  new                 |   Component:  django.newforms                  
                
 Version:  SVN                 |    Keywords:  MultiValueField MultiWidget 
clean_data decompress
   Stage:  Unreviewed          |   Has_patch:  0                                
                
-------------------------------+--------------------------------------------
 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.']}
 }}}

-- 
Ticket URL: <http://code.djangoproject.com/ticket/4284>
Django Code <http://code.djangoproject.com/>
The web framework for perfectionists with deadlines
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to