>>> class MyForm(Form):
... postdate = CompositeField(ChoiceField(choices=['Jan', ...]),
... ChoiceField(choices=range(1,32)),
... ChoiceField(choices=range(2000,2010)))
...
>>> str(MyForm())
'<tr><td>Postdate</td><td><select name="postdate1"><option>Jan</option>'
(and so forth, with select fields for all three before closing the td/tr))
If clean() were called on my hypothetical composite field above, it
would return a single datetime object.
Is there a way to obtain this behavior?
--
Brian
Yes, I'm pretty sure this is possible by writing a custom Widget
class. I'll try to implement your example and check it into a new file
django/newforms/extra/widgets.py. Stay tuned...
Adrian
--
Adrian Holovaty
holovaty.com | djangoproject.com
I haven't seen that file appear yet. Have you been able to take a look
at this? Should it just be a matter of overriding render and
id_for_label to return the composited widgets?
--
Brian
I haven't had a chance to finish this yet. Yes, it should only be a
matter of overriding render(), id_for_label() and
value_from_datadict().
Merry Christmas! In changeset [4236], I've implemented this as
django.newforms.extras.SelectDateWidget. See the new unit tests for
example usage:
http://code.djangoproject.com/changeset/4236
I still need to add unit tests that demonstrate how the value is split
into three HTML fields but combined into a single datetime.date object
by the widget value_from_datadict() method.
My original element in the form class is this:
date_opened = forms.DateField(initial=date.today())
This works as expected and if the data supplied when the form is
initialized provides a date value for date_opened, it uses the supplied
value. If it does not, it uses today's date. Note that it is a
required field.
Now, if I use this instead:
date_opened = forms.DateField(
initial=date.today(),widget=SelectDateWidget)
This does render the composite widget just fine, but it will not use a
date_opened value that is supplied with the form class is instantiated
and so displays the error saying that a required field is missing.
Any thoughts?
Thanks!
--gordon
When you pass the data to the Form constructor, you'll have to pass
three separate values -- 'date_opened_month', 'date_opened_day' and
'date_opened_year' -- rather than one value 'date_opened'. This is
necessary because the SelectDateWidget requires three values instead
of one. Make sense?