class someInputForm(forms.Form):
def __init__(self, protectedFields, *args, **kwargs):
super(someInputForm, self).__init__(*args, **kwargs)
for key, value in protectedFields.iteritems(): # For Python 2.6 just use items()
self.fields[key] = forms.CharField(initial = value, widget = forms.HiddenInput())
item = value
def cleanthis(item = item): # Sometimes you have to decide if you love Python for this or not ;-)
return item
setattr(self, 'clean_%s' %(key), cleanthis)
# Example:
hiddenData = {'action': 'add', 'hash': '05ff2cba6e002b09288a99701ad5cfc9'}
form = someInputForm(hiddenData)
Comments
Am I the only person confused by this?
#
Nope... I am also scratching my head...
#
I updated the documentation to clarify a few things for the people scratching their heads ... or not being very familiar how python works (setattr) and how to use django forms in a 'complicated' environment.
#
It creates pseudo anonymous functions that are put into the clean portion of the form for all the protected/hidden fields.
So that when they get "cleaned" they get the value that they had when they left the server, instead of the value they had when the came back from the browser.
Interesting idea, but I have to wonder, if, at the point the form is being returned from the browser, you know those values, why, did you ever send them to the browser in the first place?
#
mark0978: You want to send hidden data if there is some javascript picking it up ... doing some magic with it.
I'm used to ignore those fields but I have seen a lot of django based applications which carry some state from one step to another. Hidden form fields are obviously the wrong way to do it (use sessions instead!!), but that doesn't stop people still doing it.
#