[Zope] Solution for 'in requires character as left operand' error
Jon Whitener
wmmail@twmi.rr.com
Mon, 06 Jan 2003 16:12:49 -0500
(It took me a while to figure this out, so I submit it to the list for
future Googlers. This was experienced on Zope 2.6.)
If you are getting an error in Zope that looks like this:
'in ' requires character as left operand
...then perhaps your problem is like mine. The error occurred after I
would add an object (using a Zope Product 'addForm') in which I would
select only one single choice from a 'multiple field'. If I later
tried to access the object's properties via its Properties view method
'propertysheets/myProps/manage', I would get the above error.
(In my case, the multiple field appeared on my home-made addForm as a
number of check boxes. Each checkbox represented an item from those
in a multiple selection property created for my Product's Zclass.)
When only a single item was selected in the multiple field, it was
converted into a Python STRING, e.g.
'Music'
when what I really wanted was a "singleton" Python LIST, e.g.
['Music']
Figuring this out was the hard part. Fixing it was easy: Zope
provides a simple 'argument conversion' facility. Adding ':list' to
the addForm HTML form ensures that the multiple field value(s) will
always be treated as a Python list, even if the multiple field only
had a single value.
To illustrate the fix (similar to the one I made in my 'addForm'
HTML), assume the line with the problem property was:
<input type="checkbox" name="choices" value="choice_one" />
Change it to the following, which - via ':list' - ensures the Zope
argument conversion of 'choices' so that it is always a list:
<input type="checkbox" name="choices:list" value="choice_one" />
For more information on such Zope argument conversion, see:
http://www.zope.org/Documentation/Books/ZDG/current/ObjectPublishing.stx/
Good luck!
Jon Whitener
Detroit, Michigan USA