Discussion:
How do I get the value of a disabled Select widget?
Huuuze
2008-07-02 15:19:43 UTC
Permalink
I use the following code to disable a ModelForm field in a custom
__init__:

self.fields['state'] =
USStateField(widget=widgets.Select({'class':'disabled', 'disabled':'',
'tabindex':'-1',}, choices=STATE_CHOICES), required=False)

Unfortunately, this presents a problem. When I post the form with a
"disabled" Select field, the form is no longer valid since it expects
a value associated with State. Under non-Django conditions, I'd add a
hidden field that contains the value of the Select field and give it
the same name as the Select field. However, I'm having trouble
figuring out how to retrieve the value of the Select field to place
into a InputHidden field. This is operating under the assumption that
there isn't a better way to do this. :)

For the sake of argument, here is my ModelForm:

class BookForm(forms.ModelForm):
class Meta:
model = Book

def __init__(self, *args, **kwargs):
super(BookForm, self).__init__(*args, **kwargs)
... some other stuff happens here ....
self.fields['state'] =
USStateField(widget=widgets.Select({'class':'disabled', 'disabled':'',
'tabindex':'-1',}, choices=STATE_CHOICES), required=False)



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com
To unsubscribe from this group, send email to django-users-***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---
Huuuze
2008-07-02 15:30:23 UTC
Permalink
I figured out the puzzle:

self.fields['state'] = forms.CharField(required=False,
widget=forms.HiddenInput(), initial=self.instance.state)

Still open to feedback since I'm new to Django.
Post by Huuuze
I use the following code to disable a ModelForm field in a custom
self.fields['state'] =
USStateField(widget=widgets.Select({'class':'disabled', 'disabled':'',
'tabindex':'-1',}, choices=STATE_CHOICES), required=False)
Unfortunately, this presents a problem.  When I post the form with a
"disabled" Select field, the form is no longer valid since it expects
a value associated with State.  Under non-Django conditions, I'd add a
hidden field that contains the value of the Select field and give it
the same name as the Select field.  However, I'm having trouble
figuring out how to retrieve the value of the Select field to place
into a InputHidden field.  This is operating under the assumption that
there isn't a better way to do this.  :)
      model = Book
        super(BookForm, self).__init__(*args, **kwargs)
        ... some other stuff happens here ....
        self.fields['state'] =
USStateField(widget=widgets.Select({'class':'disabled', 'disabled':'',
'tabindex':'-1',}, choices=STATE_CHOICES), required=False)
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com
To unsubscribe from this group, send email to django-users-***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---
Jeff FW
2008-07-03 12:59:49 UTC
Permalink
Why do you need to put the value back into a form? I'm assuming that
this is on the second page of a multi-part form, or something along
those lines. A safer way to do it might be to store the value in the
session, and not rely on the data coming back from the form to be
correct. (The user can always modify the HTML of a page, and POST it
back to you.)

But then, I'm not sure what you're trying to do, so that might not
help at all.

-Jeff
Post by Huuuze
self.fields['state'] = forms.CharField(required=False,
widget=forms.HiddenInput(), initial=self.instance.state)
Still open to feedback since I'm new to Django.
Post by Huuuze
I use the following code to disable a ModelForm field in a custom
self.fields['state'] =
USStateField(widget=widgets.Select({'class':'disabled', 'disabled':'',
'tabindex':'-1',}, choices=STATE_CHOICES), required=False)
Unfortunately, this presents a problem.  When I post the form with a
"disabled" Select field, the form is no longer valid since it expects
a value associated with State.  Under non-Django conditions, I'd add a
hidden field that contains the value of the Select field and give it
the same name as the Select field.  However, I'm having trouble
figuring out how to retrieve the value of the Select field to place
into a InputHidden field.  This is operating under the assumption that
there isn't a better way to do this.  :)
      model = Book
        super(BookForm, self).__init__(*args, **kwargs)
        ... some other stuff happens here ....
        self.fields['state'] =
USStateField(widget=widgets.Select({'class':'disabled', 'disabled':'',
'tabindex':'-1',}, choices=STATE_CHOICES), required=False)
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com
To unsubscribe from this group, send email to django-users-***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Continue reading on narkive:
Loading...