I'm solving a similar problem a different way. Instead of adding a new
method to flip the tuple into a list and then back to a tuple I'm just
building my form as a list and then passing it in once I'm done
modifying it.

For example:
form_tobe = [form.Textbox('Name',
    form.notnull,
  ),
  form.Button('Submit')
]

for thing in things:
  form_tobe.insert(0, form.Textbox(thing))

real_form = form.Form(*form_tobe)

I find my way more obvious but it solves a more limited range of
problems.

On May 6, 10:33 am, Aydın ŞEN <[email protected]> wrote:
> I needed to manipulate form (add new input element) after creation but
> couldn't find a way in Form class.
>
> Scenario:
>
> You have categories and put them in a form as checkbox. Here is the form:
>
> categoryForm = form.Form(
>
> >     form.Textbox("header",
> >         form.notnull,
> >         description = "Header",
> >     ),
>
>     --other inputs--
>
> > )
>
> We had categories from db.
>
> categories = [["1","Python"],["2","Js"],["3","Html"]]
>
>
>
> > for id, name in categories:
> >     chk = form.Checkbox('categories',
> >         value = id,
> >         description = net.websafe(name)
> >     )
> >     categoryForm.add_input(chk)
>
> Thu usage is above, i defined "add_input" function in Form.py, it takes an
> Input object and index, if index is not given the Input will be append to
> the form (as last element).
>
> Here is the add_input function:
>
>     def add_input(self, input, index = None):
>
> >         """appends or inserts inputs to the form, if index is given then
> > the
> >         the input will be insert with the given index, if not the input
> > will
> >         be append to the self.inputs tuple"""
> >         self.inputs = list(self.inputs)
> >         if not index and index != 0:
> >             self.inputs.append(input)
> >         else:
> >             self.inputs.insert(index, input)
>
> >         self.inputs = tuple(self.inputs)
>
> And here is the patch:
>
> *** 109,114 ****
>
>
>
>
>
> > --- 109,126 ----
> >           return utils.storage([(i.name, i.get_value()) for i in
> > self.inputs])
> >       d = property(_get_d)
>
> > +     def add_input(self, input, index = None):
> > +         """appends or inserts inputs to the form, if index is given then
> > the
> > +         the input will be insert with the given index, if not the input
> > will
> > +         be append to the self.inputs tuple"""
> > +         self.inputs = list(self.inputs)
> > +         if not index and index != 0:
> > +             self.inputs.append(input)
> > +         else:
> > +             self.inputs.insert(index, input)
> > +
> > +         self.inputs = tuple(self.inputs)
> > +
> >   class Input(object):
> >       def __init__(self, name, *validators, **attrs):
> >           self.name = name
>
> --
> Aydın ŞEN
>
> --
> You received this message because you are subscribed to the Google Groups 
> "web.py" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to 
> [email protected].
> For more options, visit this group 
> athttp://groups.google.com/group/webpy?hl=en.

-- 
You received this message because you are subscribed to the Google Groups 
"web.py" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/webpy?hl=en.

Reply via email to