I think you wrote to the SQLObject list, but meant to write to the
FormEncode list
Michel Thadeu wrote:
> Ian, after the recent thread about form validation I think to try
> FormEncode, I used and still use FunFormKit for my projects, I use the
> FFK form generation and I confess it limitates my possibilities.
>
> I like the way htmlfill work, I tried to make a validator to a form
> with sucess, my only problem was when I try to use htmlfill. I will put
> my code on the end of message.
>
> I must question you that are using FormEncode with Webware, what is the
> best way to work with the two packages? I think in a way:
Well, I'm still working out some of it (while trying not to make it too
automatic, and in the process mysterious). But here's more or less how
an edit form for a database object works. I'm realizing it's not that
short... sigh.
One app works like this:
def setup(self):
self.form_fields = self.request().fields()
if (self.form_fields.get('id', 'new') != 'new'
and not self.form_fields.get('submit')):
# fill in defaults...
python_data = self.schema.event_to_python(
db.Event.get(int(self.form_fields['id'])))
# note event_to_python is something I coded elsewhere
self.form_fields = variabledecode.variable_encode(
self.schema.from_python(python_data))
result, rendered_form = self.form()
if result is not None:
# successful submission, again python_to_event coded elsewhere
new_event = self.schema.python_to_event(result)
self.redirect(...)
def form(self):
fields = self.form_fields
errors = {}
if fields.has_key('submit'):
try:
del fields['submit'] # not part of the schema
result = self.schema.to_python(
variabledecode.variable_decode(fields))
except Invalid, e:
errors = variabledecode.variable_encode(e.unpack_errors())
else:
return result, None
# 'submit' is a hidden
fields['submit'] = 'true'
p = htmlfill.FillingParser(fields, errors=errors, use_all_keys=True)
# I actually use ZPT to render the form, but not the defaults...
p.feed(self.get_html())
p.close()
form = p.text()
return None, form
> First I will check for errors in the writeHTML method, if the form was
> processed and there is an error, I will define an attribute with this
> error. If there is no error, I can call a formProcessed function...
>
> Then I should write the content of the page, the form is part of the
> page (using self.write to write it). I want to trigger the page
> writing, I want to get the content before write and then parse it with
> htmlfill, filling default values and error messages. Does anyone know
> how can I trigger the page writing? I use a subclass of the Cheetah's
> Template, not Page.
>
> Sorry any english mistake, my htmlfill try are the follow:
>
> #:: FormEncode test...
> from validator.schema import Schema
> from validator import validators
> from validator import htmlfill
>
> class MyValidator(Schema):
> name=validators.String(not_empty=True)
> age=validators.Int()
>
> form='''\
> <form method="post">
> <table>
> <tr>
> <td>Name: <form:error name="name"></td><td><input type="text"
> name="name"></td>
> <form:iferror name="name">mndfnaofdas</form:iferror>
> </tr>
> <p><input type="submit" name="_action_send" value="Send Data"></p>
> </form>'''
>
> userInput={'name':'michel', 'age':20}
>
> errorDict={}
> try:
> userInput=validators.to_python(MyValidator, userInput)
> except validators.Invalid, error:
> errorDict=error.error_dict
I think this should be errorDict = error.unpack_errors(); especially if
the form has nested items (though this one doesn't). Otherwise it looks
fine, except for the bug you found.
> parser=htmlfill.FillingParser(userInput, errors=errorDict)
> parser.feed(form)
> print parser.text()
> parser.close()
>
> ...
>
> I get the follow error:
>
> Traceback (most recent call last):
> File "teste.py", line 29, in ?
> parser.feed(form)
> File "/usr/lib/python2.3/site-packages/validator/htmlfill.py", line
> 68, in feed
> HTMLParser.HTMLParser.feed(self, data)
> File "/usr/lib/python2.3/HTMLParser.py", line 108, in feed
> self.goahead(0)
> File "/usr/lib/python2.3/HTMLParser.py", line 150, in goahead
> k = self.parse_endtag(i)
> File "/usr/lib/python2.3/HTMLParser.py", line 329, in parse_endtag
> self.handle_endtag(tag.lower())
> File "/usr/lib/python2.3/site-packages/validator/htmlfill.py", line
> 127, in handle_endtag
> self.handle_end_iferror()
> TypeError: handle_end_iferror() takes exactly 2 arguments (1 given)
>
> Thanks for any help!
I guess I haven't been using iferror, the formatter mostly works for me.
Anyway, that was just a bug, I committed a fix to it.
--
Ian Bicking / ia...@co... / http://blog.ianbicking.org
|