Randall Randall wrote:
>> This should be no problem; we do it all the time. Just make sure that
>> you're using different submit methods (actions) for each one.
> That seems tricky; I'd had every form using the same submit method
> and the method was cycling through every form in self._forms to
> find the successful one that had called it.
That should be fine; I'd argue that it makes your code simpler to debug
and read if each "action" has its own method, but there's no reason that
you can't iterate through your forms in your submit method, and find
which one has been posted. FYI, each form has a hidden field called
"__formID__" which allows the form processor to know which form that has
been posted. You can use that, if you want.
> So, to do this, I'd need to define a generic method and use
> setattr to set the name of each copy to the name of the
> submit button as it's generated?
I'm not sure what you mean; look up the way actions work in webkit; it
does getattr for you already, associating "actions" (based on submit
buttons) with methods. Works great.
> I use the FormKitMixIn, and self.resetForms() , but my
> problem hasn't been the contents being uncleared, but
> extra forms. That is, if I use
> f = Form.Form()
> in awake(), the second load has two forms dumped by
> for form in self._forms:
> wr( form.dump() )
> and the third time I reload the page, I have three
> forms on it, etc. I hope that using different submit
> methods, as mentioned above, fixes this. :)
Unless you do self.addForm( f ) on your FormKitMixIn derived page, this
shouldn't happen. You must be doing something wrong. Declaring a form in
awake doesn't do anything; it just makes a class instance. If you don't
register it to the servlet with self.addForm() or store it in self, it
will be destroyed when the method is over. Standard python stuff.
>>> Is there a right way to handle variable numbers of
>>> anonymous forms with FormKit, or should I try to
>>> shove all possible objects into one form?
>> Look in the examples for the "MultiFormExample.py" and follow that
> Those are named forms, though. The example didn't seem
> to apply for a variable number of forms, only a static
There's nothing special about form objects (or field objects for that
matter); they're just python class instances. You can do whatever you
want with them... stick them on the right side of dictionary, in a list,
in tuple, whatever. We did happen to name them, but you could also just
stack them up in a list, and add and delete them as you go. If you want
them to be processed automatically (you can process them manually of
course) you need to add them to the servlet via the self.addForm(). We
don't have a companion self.deleteForm() which may seem stupid. You can
certainly dig into the _forms container, if you wish.
What you are doing is a little more sophisticated than a typical servlet
example. You don't have to use the MixIn, so maybe you shouldn't. You
can look in "NoMixInExample" or even "NoServletExample" to see how FK
works, and maybe it will be easier for you to handle the processing
manually. It's no problem, really.
I hope this helps. I could look at your code, if you're still stuck.