ok,
simple example:
we need a form, that consists of questions.
question is a django model and we can have any number of them.
let's assume, that answer is a string, so we can use text widget
(in my project type of field varied for questions).

in manipulator's __init__ we iterate all questions to add fields to it.

also we should somehow pick a validator, to validate current field.
it could be some ValidatorBuilder class, or 'if-elif' statement.

i am not sure about {{ field.formfield.foo }}. looking through the
oldforms' code i guess this should work, i used other overburden
technique.


manipulators.py:

from django import oldforms as oldforms

class DynamicFormManipulator( oldforms.Manipulator ):
   def __init__( self ):
       # create field for every question and append to manipulator's
fields
       for q in Question.objects.all():
           # create validator based on question's property
           validator = ValidatorBuilder( q.something )
           field = oldforms.TextField(
                                       field_name = q.id,
                                       length = 20,
                                       max_length = 255,
                                       is_required = True,
                                       #or
                                       #is_required = q.is_required,
                                       validator_list = [ validator ]
                                     )
           field.question_text = q.question_text
           field.foo = q.foo
           # etc
           self.fields.append( field )


views.py:

def view( request ):
   manipulator = DynamicFormManipulator()
   form = oldforms.FormWrapper( manipulator, new_data, errors )
   return render_to_response( 'form.html', { 'form': form, } )


form.html:

<table>
 <thead>
   <tr>
     <th>question</th>
     <th>foo</th>
     <th>your answer</th>
     <th>&nbsp;</th>
   </tr>
 </thead>
 <tbody>
 {% for field in form.fields %}
   <tr>
     <tr>{{ field.formfield.question_text }}</tr>
     <tr>{{ field.formfield.foo }}</tr>
     <tr>{{ field }}</tr>
     <tr>
       {% if field.errors %}
       <span class="error">
         *** {{ field.errors|join:", " }}
       </span>
       {% else %}
         &nbsp;
       {% endif %}
     </tr>
   </tr>
 {% endfor %}
 </tbody>
</table>


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

Reply via email to