formencode.htmlfill

9 views
Skip to first unread message

Shannon -jj Behrens

unread,
Sep 25, 2006, 6:06:11 PM9/25/06
to gen...@googlegroups.com
Hi,

I'm using FormEncode for form validation. Part of FormEncode is
formencode.htmlfill which is used to fill in defaults and error
messages during form generation. Of course, using htmlfill would be
silly since Genshi already has the template parsed. I've seen
<http://genshi.edgewall.org/wiki/GenshiRecipes/FormFilling> which has
apparently been updated very recently.

I think this recipe should perhaps be polished a bit and shoved into
the library. It would be less than ideal if we each had our own
version of this code. :-/

I recently wrote my standard "test" application in Pylons + Cheetah,
and the only thing I could see that needed work was the form
generation. The direction I'm heading down is doing things like:

<field name="name">
<label>Name</label>
<input type="text">
</field>

Which is a "macro" powered by py:match that can rearrange all of its
parts and perhaps come up with something like:

<tr>
<td><label for="name">Name</label></td>
<td>
<input type="text" value="${get_default('name')}"></td>
<span class="error">${get_error('name')</span>
</td>
</tr>

Of course, users are encouraged to write macros for how they want
their forms to look.

Basically, I'm trying to apply DRY to form generation. Suggestions are welcome.

Thanks,
-jj

--
The one who gets the last laugh isn't the one who did the laughing,
but rather the one who did the writing.

Christopher Lenz

unread,
Sep 26, 2006, 6:09:01 AM9/26/06
to gen...@googlegroups.com
Hi Shannon,

Am 26.09.2006 um 00:06 schrieb Shannon -jj Behrens:
> I'm using FormEncode for form validation. Part of FormEncode is
> formencode.htmlfill which is used to fill in defaults and error
> messages during form generation. Of course, using htmlfill would be
> silly since Genshi already has the template parsed. I've seen
> <http://genshi.edgewall.org/wiki/GenshiRecipes/FormFilling> which has
> apparently been updated very recently.
>
> I think this recipe should perhaps be polished a bit and shoved into
> the library. It would be less than ideal if we each had our own
> version of this code. :-/

I agree. I've been working on enhancing the HTMLFormFiller filter
(the second example on that page) and adding unit tests. It's likely
that it'll be rolled into Genshi as genshi.filters.HTMLFormFiller for
the next release.

Feedback on that filter is welcome, especially if you have experience
with formencode/htmlfill.

> I recently wrote my standard "test" application in Pylons + Cheetah,
> and the only thing I could see that needed work was the form
> generation. The direction I'm heading down is doing things like:
>
> <field name="name">
> <label>Name</label>
> <input type="text">
> </field>
>
> Which is a "macro" powered by py:match that can rearrange all of its
> parts and perhaps come up with something like:
>
> <tr>
> <td><label for="name">Name</label></td>
> <td>
> <input type="text" value="${get_default('name')}"></td>
> <span class="error">${get_error('name')</span>
> </td>
> </tr>
>
> Of course, users are encouraged to write macros for how they want
> their forms to look.
>
> Basically, I'm trying to apply DRY to form generation. Suggestions
> are welcome.

I think that approach is okay as long as you don't care whether
you're templates are valid and basically usable HTML (because the
<field> elements breaks that). I'd still leave the filling in of
values to the HTMLFormFiller (once it becomes available, that is).
The logic required is better implemented in Python code IMO.

Cheers,
Chris
--
Christopher Lenz
cmlenz at gmx.de
http://www.cmlenz.net/

Shannon -jj Behrens

unread,
Sep 26, 2006, 7:31:49 PM9/26/06
to gen...@googlegroups.com
On 9/26/06, Christopher Lenz <cml...@gmx.de> wrote:
>
> Hi Shannon,
>
> Am 26.09.2006 um 00:06 schrieb Shannon -jj Behrens:
> > I'm using FormEncode for form validation. Part of FormEncode is
> > formencode.htmlfill which is used to fill in defaults and error
> > messages during form generation. Of course, using htmlfill would be
> > silly since Genshi already has the template parsed. I've seen
> > <http://genshi.edgewall.org/wiki/GenshiRecipes/FormFilling> which has
> > apparently been updated very recently.
> >
> > I think this recipe should perhaps be polished a bit and shoved into
> > the library. It would be less than ideal if we each had our own
> > version of this code. :-/
>
> I agree. I've been working on enhancing the HTMLFormFiller filter
> (the second example on that page) and adding unit tests. It's likely
> that it'll be rolled into Genshi as genshi.filters.HTMLFormFiller for
> the next release.
>
> Feedback on that filter is welcome, especially if you have experience
> with formencode/htmlfill.

Thanks. I'm very much looking forward to this. It sounds like you
know what you're doing, but if there's anything particular you need
help with, I'm here to help. I need this sooner rather than later.

> > I recently wrote my standard "test" application in Pylons + Cheetah,
> > and the only thing I could see that needed work was the form
> > generation. The direction I'm heading down is doing things like:
> >
> > <field name="name">
> > <label>Name</label>
> > <input type="text">
> > </field>
> >
> > Which is a "macro" powered by py:match that can rearrange all of its
> > parts and perhaps come up with something like:
> >
> > <tr>
> > <td><label for="name">Name</label></td>
> > <td>
> > <input type="text" value="${get_default('name')}"></td>
> > <span class="error">${get_error('name')</span>
> > </td>
> > </tr>
> >
> > Of course, users are encouraged to write macros for how they want
> > their forms to look.
> >
> > Basically, I'm trying to apply DRY to form generation. Suggestions
> > are welcome.
>
> I think that approach is okay as long as you don't care whether
> you're templates are valid and basically usable HTML (because the
> <field> elements breaks that).

Yeah, if I was really worried about that, I guess I'd use an XML namespace.

> I'd still leave the filling in of
> values to the HTMLFormFiller (once it becomes available, that is).
> The logic required is better implemented in Python code IMO.

Agreed. The macro should be built "on top" of the HTMLFormFiller.

Pedro Algarvio, aka, s0undt3ch

unread,
Oct 21, 2006, 3:01:50 PM10/21/06
to Genshi
So how does one avoid formencode.htmlfill from filling our parsed
template?

We can't include:
> <form:iferror name="field_name">...</form:iferror>
in the templates because that causes genshi to throw a parser error.

So, how do we handle this?

Thanks!
Pedro Algarvio.

Shannon -jj Behrens

unread,
Oct 21, 2006, 3:24:52 PM10/21/06
to gen...@googlegroups.com

Remember that formencode.htmlfill must reparse the generated HTML.
That just sounds silly and wasteful to me. In a previous thread,
someone said that this recipe
<http://genshi.edgewall.org/wiki/GenshiRecipes/FormFilling> was going
to be added to Genshi itself. That's a good start.

On top of that, I planned on using the match mechanism to create
something "macro-ish":

<!-- Here's a macro for a field. -->
<py:match path="field">
Label: ${select('label')}<br />
Field: ${select('input')}<br />
Name: ${select('input/@name')}<br />
Errors: ${errors.get(str(select('input/@name')), "")}<br />
</py:match>

<!-- Here's how to use it.

The py:with is just to set some variables that would normally be
set in the controller
-->
<form py:with="errors = {
'dude': 'Big error, man.'
}
">
<field>
<label>The Label</label>
<input type="text" name="dude" />
</field>
</form>

Does that make sense? The idea is to create more functionality with
less actual markup.

Best Regards,

Matt Good

unread,
Oct 22, 2006, 7:21:54 PM10/22/06
to Genshi
Shannon -jj Behrens wrote:
> On 10/21/06, Pedro Algarvio, aka, s0undt3ch <u...@ufsoft.org> wrote:
> > So how does one avoid formencode.htmlfill from filling our parsed
> > template?
> >
> > We can't include:
> > > <form:iferror name="field_name">...</form:iferror>
> > in the templates because that causes genshi to throw a parser error.
> >
> > So, how do we handle this?
>
> Remember that formencode.htmlfill must reparse the generated HTML.
> That just sounds silly and wasteful to me. In a previous thread,
> someone said that this recipe
> <http://genshi.edgewall.org/wiki/GenshiRecipes/FormFilling> was going
> to be added to Genshi itself. That's a good start.
>
> On top of that, I planned on using the match mechanism to create
> something "macro-ish":

I've done something similar with my formencode validaion in Pylons.
It's a pretty simple match template to match form fields with error
messages and insert a <span> before the field with the message:

<py:if test="c.errors">
<div py:match="input|textarea|select" py:strip="" py:with="error =
c.errors.get(str(select('@name')))">
<span class="error-message" py:if="error" py:content="error"></span>
${select('.')}
</div>
</py:if>

-- Matt Good

Pedro Algarvio, aka, s0undt3ch

unread,
Oct 25, 2006, 8:54:26 PM10/25/06
to Genshi

Matt, are you also using the validate decorator(Pylons), created your
own decorator(in this case, care to share?), or not using a decorator
at all?

With the last approach, how do you handle that match template paths?

Thanks.

Reply all
Reply to author
Forward
0 new messages