newforms: Creating a Widget with multiple HTML form elements

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

newforms: Creating a Widget with multiple HTML form elements

Thomas Steinacher

Hello,

Sometimes it would useful to create a FormField with a Widget that
consists of multiple HTML form elements. Rendering such a widget is
very easy, however, I don't see a possibility to get the data in the
clean() method of the Field. There should be an easy way to get the
data. Maybe by assigning multiple names for a field?

Consider the example below. I need this contact info in multiple forms
and I don't want to define all three fields in every form.


class ContactInfoWidget(forms.Widget):
    def render(self, name, value, attrs=None):
        print name
        TYPES = (
                (_('Main contact'), 'main_contact'),
                (_('Billing contact'),'billing_contact'),
                (_('Technical contact'), 'technical_contact'),
            )
        emails = ('your@email','other@mail')

        return '<div class="contact_info">\n%s</div>\n' % (
                ''.join(['  <div class="float">%s:</div> <select
name="%s_%s">%s</select> <a href="#">%s</a><br/ >\n' %
                (type, name, type_name, ''.join(['<option>%s</option>'
% email for email in emails]), _('Other...'))
                for type, type_name in TYPES]))

class ContactInfoField(forms.Field):
    widget = ContactInfoWidget

    def __init__(self, required=True, widget=None, label=_('Contact
info')):
        forms.Field.__init__(self, required, widget, label)
    def clean(self, value):
        print value
        return value


Thomas Steinacher


--~--~---------~--~----~------------~-------~--~----~
 You received this message because you are subscribed to the Google Groups "Django developers" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/django-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: newforms: Creating a Widget with multiple HTML form elements

Adrian Holovaty-3

On 12/27/06, Thomas Steinacher <[hidden email]> wrote:
> Sometimes it would useful to create a FormField with a Widget that
> consists of multiple HTML form elements. Rendering such a widget is
> very easy, however, I don't see a possibility to get the data in the
> clean() method of the Field. There should be an easy way to get the
> data. Maybe by assigning multiple names for a field?

Hi Thomas,

Check out the SelectDateWidget code in
django/newforms/extras/widgets.py. That's an example of a widget with
multiple HTML form elements. The key is to implement the
value_from_datadict() method, which combines the values from the
multiple elements into a single value. Does this answer your question?

Adrian

--
Adrian Holovaty
holovaty.com | djangoproject.com

--~--~---------~--~----~------------~-------~--~----~
 You received this message because you are subscribed to the Google Groups "Django developers" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/django-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: newforms: Creating a Widget with multiple HTML form elements

Honza Král
In reply to this post by Thomas Steinacher
have you tried overriding value_from_datadict() on the custom widget?
that should do the trick

since the widget is responsible for rendering, it is also the only
thing that can retrieve the data from the datadict (its simply the
only thing aware of the data format)...

On 12/27/06, Thomas Steinacher <[hidden email]> wrote:

>
> Hello,
>
> Sometimes it would useful to create a FormField with a Widget that
> consists of multiple HTML form elements. Rendering such a widget is
> very easy, however, I don't see a possibility to get the data in the
> clean() method of the Field. There should be an easy way to get the
> data. Maybe by assigning multiple names for a field?
>
> Consider the example below. I need this contact info in multiple forms
> and I don't want to define all three fields in every form.
>
>
> class ContactInfoWidget(forms.Widget):
>     def render(self, name, value, attrs=None):
>         print name
>         TYPES = (
>                 (_('Main contact'), 'main_contact'),
>                 (_('Billing contact'),'billing_contact'),
>                 (_('Technical contact'), 'technical_contact'),
>             )
>         emails = ('your@email','other@mail')
>
>         return '<div class="contact_info">\n%s</div>\n' % (
>                 ''.join(['  <div class="float">%s:</div> <select
> name="%s_%s">%s</select> <a href="#">%s</a><br/ >\n' %
>                 (type, name, type_name, ''.join(['<option>%s</option>'
> % email for email in emails]), _('Other...'))
>                 for type, type_name in TYPES]))
>
> class ContactInfoField(forms.Field):
>     widget = ContactInfoWidget
>
>     def __init__(self, required=True, widget=None, label=_('Contact
> info')):
>         forms.Field.__init__(self, required, widget, label)
>     def clean(self, value):
>         print value
>         return value
>
>
> Thomas Steinacher
>
>
> >
>


--
Honza Kr l
E-Mail: [hidden email]
ICQ#:   107471613
Phone:  +420 606 678585

--~--~---------~--~----~------------~-------~--~----~
 You received this message because you are subscribed to the Google Groups "Django developers" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/django-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: newforms: Creating a Widget with multiple HTML form elements

Jim Fritchman
In reply to this post by Adrian Holovaty-3

Adrian,

Would you use a similar method if you wanted a sub form?  I need to
create a list of addresses which the user can add/edit or delete from
within a form that they are adding\editing their other personal
information.

Thanks for your help,
Jim

On Dec 27, 1:40 pm, "Adrian Holovaty" <[hidden email]> wrote:

> On 12/27/06, Thomas Steinacher <[hidden email]> wrote:
>
> > Sometimes it would useful to create a FormField with a Widget that
> > consists of multiple HTML form elements. Rendering such a widget is
> > very easy, however, I don't see a possibility to get the data in the
> > clean() method of the Field. There should be an easy way to get the
> > data. Maybe by assigning multiple names for a field?Hi Thomas,
>
> Check out the SelectDateWidget code in
> django/newforms/extras/widgets.py. That's an example of a widget with
> multiple HTML form elements. The key is to implement the
> value_from_datadict() method, which combines the values from the
> multiple elements into a single value. Does this answer your question?
>
> Adrian
>
> --
> Adrian Holovaty
> holovaty.com | djangoproject.com


--~--~---------~--~----~------------~-------~--~----~
 You received this message because you are subscribed to the Google Groups "Django developers" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/django-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: newforms: Creating a Widget with multiple HTML form elements

Adrian Holovaty-3

On 12/28/06, [hidden email] <[hidden email]> wrote:
> Would you use a similar method if you wanted a sub form?  I need to
> create a list of addresses which the user can add/edit or delete from
> within a form that they are adding\editing their other personal
> information.

I'm not sure what you mean here...

Adrian

--
Adrian Holovaty
holovaty.com | djangoproject.com

--~--~---------~--~----~------------~-------~--~----~
 You received this message because you are subscribed to the Google Groups "Django developers" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/django-developers?hl=en
-~----------~----~----~----~------~----~------~--~---