Ian Bicking: the old part of his blog

2005 01 htmlfill comment 000

It's pretty straight forward:

from validator import htmlfill
parser = htmlfill.FillingParser({'lname': 'Doe'}, errors={'fname': 'Please enter a name'})
parser.feed('''<form action="">
<form:error name="fname">
First: <input type="text" name="fname"><br>
<form:error name="lname">
Last: <input type="text" name="lname"><br>''')
print parser.text()

Output (well, slightly reformatted, since it eats newlines right now for some reason):

<form action="">
<span class="error">Please enter a name</span><br>
First: <input type="text" name="fname" value=""><br>
Last: <input type="text" name="lname" value="Doe">

The errors are formatted using htmlfill.default_formatter, or you can use format="formatter_name" to specify your custom formatter. I'd like to include errors by default (if you don't include a specific <form:error> tag), but I haven't figured out how to do that properly; you'd have to double-parse the form, to check which errors were left unused. For now it (optionally) gives you an exception if you include an error without a corresponding <form:error> tag, but this has been problematic.

You can also use <form:iferror name="field_name"> to conditionally include text, in case you need to put a <br> or somesuch into the markup only if you have an error. But for the most part default_formatter (or your custom formatter) is an easier way to put in such tags.

Comment on Re: htmlfill
by Ian Bicking