from django import newforms as forms
class DynForm(forms.Form):
"""
Dynamic form that allows the user to change and then verify the data that was parsed
"""
def setFields(self, kwds):
"""
Set the fields in the form
"""
keys = kwds.keys()
keys.sort()
for k in keys:
self.fields[k] = kwds[k]
def setData(self, kwds):
"""
Set the data to include in the form
"""
keys = kwds.keys()
keys.sort()
for k in keys:
self.data[k] = kwds[k]
def validate(self, post):
"""
Validate the contents of the form
"""
for name,field in self.fields.items():
try:
field.clean(post[name])
except ValidationError, e:
self.errors[name] = e.messages
#### In the view ########################################################
# Form definition
# kwargs is a dictionary. The key being the name of the field and the value
# being the type (CharField(kwargs*))
kwargs['a_name'] = forms.CharField(label="Name", max_length=25, help_text="name")
kwargs['b_lname'] = forms.CharField(label="Last Name", help_text="lname")
kwargs['c_bday'] = forms.DateField(label="Birthday", help_text="birthday")
# Creating the form object and manipulating/validating it
form = DynForm() # Create the form
form.setFields(kwargs) # Set the fields as defined in the kwargs dictionary
form.setData(request.POST) # Set the form data
form.validate(request.POST) # validate the from
##########################################################################
Comments
This is great, but to make it work you need to modify setData to add the line:
self.is_bound = True
Otherwise when you display the form it ignores the data
#
How can i use this? thanks
#
Nice. This came in handy, but it doesn't play well with multivalue fields.
I had to modify setData and validate to deal with this situation:
#
I couldn't find how I can access the cleaned_data and in fact why you are creating a different method validate instead of using self.full_clean() or something similar. I just changed the validate to:
def validate(self): self.full_clean()
for readability sake and now I can access:
dynform_instance.cleaned_data['myfield']
#
My modifications for accessing cleaned_data:
#
Hi, imho it is good to add to it a function "is_valid". So form can be used as always.
#
Could you please explain, how can change repesentation of form in html?
I can easily wright {{form}} or {{form.as_table}}, but I want locate them by myself.
Should I do smth like {{form.fields[k]}} ? (I can't make this work)
#