Re: Make htmlfill only apply to certain named forms

Adam Batkin <[email protected]> Tue, 02 Feb 2010 19:37:17 +0000
Newsgroups gmane.comp.python.formencode
Message-ID <[email protected]>
On 02/01/2010 11:34 PM, Ian Wilson wrote:
> This idea seems okay but like its going down the wrong path.  If
> someone has two forms on a page with conflicting keys will they just
> run htmlfill twice, once for each form?  What about three forms?  Then
> we get the problem of removing or leaving the defaults in the html
> already, etc.  If htmlfill handles multiple forms at once will
> defaults then need to be partitioned by form id/name?

Yes, under my proposed solution, you could run htmlfill multiple times, 
and if each form has a name (and a form_name is passed to render()) then 
each pass would modify the defaults of a different form, leaving the 
others intact. So you would partition your defaults (and calls to 
render() by form name.

Actually, my patch will need some cleaning up to do exactly that, as 
currently it deals with the named form properly, but for any other forms 
on the page, it strips <form:error> and <form:iferror> tags (unless the 
<form:iferror name="not something">). I suppose an extra option may be 
warranted to have it leave those alone.

> Seems like all this could be solved if htmlfill.render received only
> one form via a templating function within whatever templating language
> you are using.  This would be more explicit and might perform
> better(Although I don't know much about performance).  If you had two
> forms you could just pass them to two seperate calls of
> htmlfill.render.  I probably am missing something very obvious but
> what blocks/deters people from doing this?

That sounds like it would definitely work, and without any modifications 
to htmlfill. My proposed change also wouldn't affect the ability to do 
that. But dealing with multiple forms that way requires that you 
structure your templates in a very specific way. And also some web 
frameworks don't make that process completely natural.

The other possibility would be to optionally pass a dict to defaults, 
errors and add_attributes, where each key would be a form name, and the 
value would be the defaults, errors and add_attributes for forms of that 
name. Then a single pass with render() could take care of all forms.

Thanks,

-Adam Batkin

------------------------------------------------------------------------------
The Planet: dedicated and managed hosting, cloud storage, colocation
Stay online with enterprise data centers and the best network in the business
Choose flexible plans and management services without long-term contracts
Personal 24x7 support from experience hosting pros just a phone call away.
http://p.sf.net/sfu/theplanet-com