Select Field With Other option

97 views
Skip to first unread message

Paul Royik

unread,
Jan 26, 2015, 5:29:47 PM1/26/15
to django...@googlegroups.com
I need to build a form field with a special select field.
Select field should have option other on which text field is shown. After entering value in textfield, it should be validated, saved and then refered to initial form.

How this can be done?


I'm trying to override MultiValueField and MultiWidget, but no luck.

Sergiy Khohlov

unread,
Jan 28, 2015, 12:28:21 AM1/28/15
to django-users

You can not do it directly. This should be do by JavaScript's using at HTML side. All field s are added to form and some of them are hidden.

27 січ. 2015 00:29, користувач "Paul Royik" <distan...@gmail.com> написав:
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/268074d9-67af-4b19-a083-314ad1125414%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Karim

unread,
Jan 28, 2015, 12:57:25 AM1/28/15
to Django Users

On Wed, Jan 28, 2015 at 4:28 PM, Sergiy Khohlov <skho...@gmail.com> wrote:

You can not do it directly. This should be do by JavaScript's using at HTML side. All field s are added to form and some of them are hidden.

​Is not possible to use crispy forms and change the widget with the Field() object?



--
Karim N. Gorjux

trubliphone

unread,
Jan 28, 2015, 9:20:48 AM1/28/15
to django...@googlegroups.com
I have done something fairly similar.

I wound up using two separate fields (rather than a single MultiValueField).  The first one is based on a TextField and its formfield is based on a MultipleChoiceField.  On form creation of forms that use this field, I append [("_OTHER", "---OTHER---")] to the set of choices.  The second one is just a standard CharField.  That field is set to be hidden.

I add a javascript handler to the first field such that whenever its value changes, if it includes "_OTHER" the second field is displayed otherwise it is hidden.  Both fields are validated and saved as needed.  When I am working w/ the underlying models, I know to use the second field's value iff the first field's value contains "_OTHER."  If it doesn't, I just ignore it.
Reply all
Reply to author
Forward
0 new messages