|
From: Ben B. <be...@gr...> - 2005-05-18 19:55:53
|
I've been trying to use htmlfill with the default errors, but have been
unable to get it to work. It works fine when I use the <form:iferror
name="..."></form:iferror> and turn off the formatters.
However, when I use:
<form:error name="...">
I get the following error:
Error:
Error: Invalid instance has no attribute 'replace'
File:
/usr/local/lib/python2.3/cgi.py line 1033
Context:
1030:
1031: def escape(s, quote=None):
1032: """Replace special characters '&', '<' and '>' by SGML entities."""
1033: s = s.replace("&", "&") # Must be done first!
1034: s = s.replace("<", "<")
1035: s = s.replace(">", ">")
1036: if quote:
Traceback:
/usr/local/lib/python2.3/cgi.py:1033
/usr/local/lib/python2.3/site-packages/validator/htmlfill.py:5
/usr/local/lib/python2.3/site-packages/validator/htmlfill.py:173
/usr/local/lib/python2.3/site-packages/validator/htmlfill.py:118
/usr/local/lib/python2.3/HTMLParser.py:281
/usr/local/lib/python2.3/HTMLParser.py:148
/usr/local/lib/python2.3/HTMLParser.py:108
/usr/local/lib/python2.3/site-packages/validator/htmlfill.py:79
Here's a snippet from my form:
<td><fieldset class="inputfields">
<p>
<label for="login">Choose your Login *</label>
<input name="login" type="text" id="login" title="Login" />
<form:error name="login">
</p>
<p>
Is that the proper usage?
Thanks,
Ben
|
|
From: Ian B. <ia...@co...> - 2005-05-18 20:04:50
|
Ben Bangert wrote: > I've been trying to use htmlfill with the default errors, but have been > unable to get it to work. It works fine when I use the <form:iferror > name="..."></form:iferror> and turn off the formatters. > > However, when I use: > <form:error name="..."> > > I get the following error: > Error: > Error: Invalid instance has no attribute 'replace' I suspect you have to use exc.unpack_errors() to get the strings out of the exception. -- Ian Bicking / ia...@co... / http://blog.ianbicking.org |
|
From: Ben B. <be...@gr...> - 2005-05-18 20:10:13
|
On 5/18/05 1:03 PM, "Ian Bicking" <ia...@co...> wrote: > Ben Bangert wrote: >> I've been trying to use htmlfill with the default errors, but have been >> unable to get it to work. It works fine when I use the <form:iferror >> name="..."></form:iferror> and turn off the formatters. >> >> However, when I use: >> <form:error name="..."> >> >> I get the following error: >> Error: >> Error: Invalid instance has no attribute 'replace' > > I suspect you have to use exc.unpack_errors() to get the strings out of > the exception. I'm not sure what you mean. When I use the form:iferror and completely turn off the default_formatters in the FillingParser, it works fine. When I do what I mentioned, the FillingParser dies with the message I listed. Am I using the <form:error> bit incorrectly, or if you need more information to help how can I get that? Thanks, Ben |
|
From: Ian B. <ia...@co...> - 2005-05-18 20:17:29
|
Ben Bangert wrote: > On 5/18/05 1:03 PM, "Ian Bicking" <ia...@co...> wrote: > > >>Ben Bangert wrote: >> >>>I've been trying to use htmlfill with the default errors, but have been >>>unable to get it to work. It works fine when I use the <form:iferror >>>name="..."></form:iferror> and turn off the formatters. >>> >>>However, when I use: >>><form:error name="..."> >>> >>>I get the following error: >>>Error: >>>Error: Invalid instance has no attribute 'replace' >> >>I suspect you have to use exc.unpack_errors() to get the strings out of >>the exception. > > > I'm not sure what you mean. When I use the form:iferror and completely turn > off the default_formatters in the FillingParser, it works fine. When I do > what I mentioned, the FillingParser dies with the message I listed. > > Am I using the <form:error> bit incorrectly, or if you need more information > to help how can I get that? Well, I'm not really sure what's happening, except that the default formatter is trying to take an Invalid instance and format it as HTML; the errors that are passed into htmlfill should be a flat dictionary of strings. The .unpack_errors() method on Invalid instances will give you that dictionary (though it might not be flat -- you might have to run it through variabledecode.variable_decode first if you are using compound forms). The code that matters in this case is whatever is invoking htmlfill. -- Ian Bicking / ia...@co... / http://blog.ianbicking.org |
|
From: Ben B. <be...@gr...> - 2005-05-18 20:23:34
|
On 5/18/05 1:16 PM, "Ian Bicking" <ia...@co...> wrote:
> Well, I'm not really sure what's happening, except that the default
> formatter is trying to take an Invalid instance and format it as HTML;
> the errors that are passed into htmlfill should be a flat dictionary of
> strings. The .unpack_errors() method on Invalid instances will give you
> that dictionary (though it might not be flat -- you might have to run it
> through variabledecode.variable_decode first if you are using compound
> forms).
>
> The code that matters in this case is whatever is invoking htmlfill.
Ah ha. Here's what the errors dict looks like that I pass into htmlfill:
'city': <validator.api.Invalid instance at 0x98ba82c>, 'address1':
<validator.api.Invalid instance at 0x98ba80c>, 'telephone':
<validator.api.Invalid instance at 0x98ba90c>, 'zipcode':
<validator.api.Invalid instance at 0x98ba40c>, 'lname':
<validator.api.Invalid instance at 0x98ba4ec>, 'state':
<validator.api.Invalid instance at 0x98ba56c>, 'fname':
<validator.api.Invalid instance at 0x98baf6c>, 'country':
<validator.api.Invalid instance at 0x98ba72c>, 'login':
<validator.api.Invalid instance at 0x98bae4c>, 'password':
<validator.api.Invalid instance at 0x98baa8c>, 'email':
<validator.api.Invalid instance at 0x98ba62c>}
I had assumed that I could pass that in since that's what htmlform.HTMLForm
had returned to me. I'm a little confused now how I should be setting up the
form.
I currently check for errors on the form like this:
form = htmlform.HTMLForm(html,formschemas.BillShipPage())
errors = {}
form_result, errors = form.validate(self.ARGS)
I then pass the errors that returns into the htmlfill. Html in the above is
a form snippet. What is the recommended way to obtain the errors dict passed
into htmlfill?
Thanks,
Ben
|
|
From: Ian B. <ia...@co...> - 2005-05-18 20:33:52
|
Ben Bangert wrote:
> I currently check for errors on the form like this:
> form = htmlform.HTMLForm(html,formschemas.BillShipPage())
> errors = {}
> form_result, errors = form.validate(self.ARGS)
>
> I then pass the errors that returns into the htmlfill. Html in the above is
> a form snippet. What is the recommended way to obtain the errors dict passed
> into htmlfill?
I was returning the wrong thing in htmlform -- should be fixed in svn now.
--
Ian Bicking / ia...@co... / http://blog.ianbicking.org
|
|
From: Ben B. <be...@gr...> - 2005-05-18 20:43:04
|
On 5/18/05 1:32 PM, "Ian Bicking" <ia...@co...> wrote:
> Ben Bangert wrote:
>> I currently check for errors on the form like this:
>> form = htmlform.HTMLForm(html,formschemas.BillShipPage())
>> errors = {}
>> form_result, errors = form.validate(self.ARGS)
>>
>> I then pass the errors that returns into the htmlfill. Html in the above is
>> a form snippet. What is the recommended way to obtain the errors dict passed
>> into htmlfill?
>
> I was returning the wrong thing in htmlform -- should be fixed in svn now.
I just ran svn up and saw the htmlform update, however it doesn't seem to
have changed it. Should I be using something other than form.validate to
pull that flat dict of error strings out?
In the meantime this did get my form running:
newerrors = {}
for key, val in errors.iteritems():
newerrors[key] = val.unpack_errors()
parser = htmlfill.FillingParser(defaults = self.ARGS, errors =
newerrors)
- Ben
|
|
From: Ian B. <ia...@co...> - 2005-05-18 20:48:53
|
Ben Bangert wrote:
> On 5/18/05 1:32 PM, "Ian Bicking" <ia...@co...> wrote:
>
>
>>Ben Bangert wrote:
>>
>>>I currently check for errors on the form like this:
>>> form = htmlform.HTMLForm(html,formschemas.BillShipPage())
>>> errors = {}
>>> form_result, errors = form.validate(self.ARGS)
>>>
>>>I then pass the errors that returns into the htmlfill. Html in the above is
>>>a form snippet. What is the recommended way to obtain the errors dict passed
>>>into htmlfill?
>>
>>I was returning the wrong thing in htmlform -- should be fixed in svn now.
>
>
> I just ran svn up and saw the htmlform update, however it doesn't seem to
> have changed it. Should I be using something other than form.validate to
> pull that flat dict of error strings out?
>
> In the meantime this did get my form running:
> newerrors = {}
> for key, val in errors.iteritems():
> newerrors[key] = val.unpack_errors()
> parser = htmlfill.FillingParser(defaults = self.ARGS, errors =
> newerrors)
Revision 792?
def validate(self, request_dict, state=None):
schema = self.schema
try:
result = schema.to_python(request_dict, state=state)
return result, None
except Invalid, e:
return None, e.unpack_errors()
--
Ian Bicking / ia...@co... / http://blog.ianbicking.org
|
|
From: Ben B. <be...@gr...> - 2005-05-18 21:03:57
|
On 5/18/05 1:48 PM, "Ian Bicking" <ia...@co...> wrote: > Revision 792? > > def validate(self, request_dict, state=None): > schema = self.schema > try: > result = schema.to_python(request_dict, state=state) > return result, None > except Invalid, e: > return None, e.unpack_errors() Yep, am now using 792. Still seems to be returning a dictionary of the Invalid instances. On a side-note, I would recommend either changing htmlform's render function to allow an argument to tweak the use_all_keys, or defaulting it to false like how FillingParser defaults to False for that. That's actually the only reason I'm calling FillingParser directly rather than using render as in the doc examples. I'd be happy to submit a patch for this if that's the preferred way to make a suggestion. Thanks, Ben |
|
From: Ian B. <ia...@co...> - 2005-05-18 21:08:21
|
Ben Bangert wrote: > On 5/18/05 1:48 PM, "Ian Bicking" <ia...@co...> wrote: > > >>Revision 792? >> >> def validate(self, request_dict, state=None): >> schema = self.schema >> try: >> result = schema.to_python(request_dict, state=state) >> return result, None >> except Invalid, e: >> return None, e.unpack_errors() > > > Yep, am now using 792. Still seems to be returning a dictionary of the > Invalid instances. > > On a side-note, I would recommend either changing htmlform's render function > to allow an argument to tweak the use_all_keys, or defaulting it to false > like how FillingParser defaults to False for that. That's actually the only > reason I'm calling FillingParser directly rather than using render as in the > doc examples. > > I'd be happy to submit a patch for this if that's the preferred way to make > a suggestion. That would be fine. I don't know why it's not working for you, but if you want to twiddle with it and include that too, that'd be good. I haven't really used htmlform, I just put it in as a thought experiment, so it doesn't have any tests or whatnot. -- Ian Bicking / ia...@co... / http://blog.ianbicking.org |
|
From: Ben B. <be...@gr...> - 2005-05-18 22:55:06
|
On 5/18/05 2:07 PM, "Ian Bicking" <ia...@co...> wrote:
> That would be fine. I don't know why it's not working for you, but if
> you want to twiddle with it and include that too, that'd be good. I
> haven't really used htmlform, I just put it in as a thought experiment,
> so it doesn't have any tests or whatnot.
I found out why it wasn't working.... When I had switched to using the svn,
I didn't switch my import statement to use formencode rather than validator.
So I was still using the old formencode package. Once I switched, errors did
return the unpacked errors properly.
I noticed there's one minor bug(?) in the svn, as well as a pretty major one
in htmlform that you might not have noticed if your site-packages/validator/
directory is still hanging around.
Here's the patch for htmlform.py that fixes it (I also included the patch to
the render method):
Index: formencode/htmlform.py
===================================================================
--- formencode/htmlform.py (revision 792)
+++ formencode/htmlform.py (working copy)
@@ -25,7 +25,7 @@
import htmlfill
import htmlfill_schemabuilder
-from validator import Invalid
+from api import Invalid
class HTMLForm(object):
@@ -53,10 +53,10 @@
p.close()
return listener.schema()
- def render(self, defaults={}, errors={}):
+ def render(self, defaults={}, errors={}, use_all_keys=False):
p = htmlfill.FillingParser(
defaults=defaults, errors=errors,
- use_all_keys=True)
+ use_all_keys=use_all_keys)
p.feed(self.form)
p.close()
return p.text()
And here's a patch to htmlfill which skips image input types (typically used
as submit buttons):
Index: formencode/htmlfill.py
===================================================================
--- formencode/htmlfill.py (revision 792)
+++ formencode/htmlfill.py (working copy)
@@ -225,6 +225,8 @@
self.write_tag('input', attrs)
self.skip_next = True
self.add_key(name)
+ elif t == 'image':
+ pass
else:
assert 0, "I don't know about this kind of <input>: %s (pos:
%s)" \
% (t, self.getpos())
Thanks,
Ben
|