[Zope3-Users] Re: IField in Object(Field): RequiredMissing
Christian Lück
christian.lueck at ruhr-uni-bochum.de
Wed Mar 15 10:07:21 EST 2006
Daniel Nouri wrote:
>>
>> There is a nice and helpfull example in ++apidoc++: Book --> Widgets and
>> Forms --> Advanced Widgets
>> Please also refer to srichter's book, chapter 8.3 (p. 54).
>
> I don't have Stephan's book around right now.
Isn't it online on the zope3 homepage?
> But I'm using the Object
> field just as you suggest. But that doesn't work. The error is the
> same as described above. I just wanted to reproduce it in the
> interactive interpreter.
>
I'm not sure if you use it like it was intended to be used ;)
Here we go on the command line:
clueck at aeolos:~$ export PYTHONPATH=/usr/local/Zope-3.2.0b2/lib/python/
clueck at aeolos:~$ python2.4
Python 2.4.2 (#1, Jan 1 2006, 20:27:29)
[GCC 3.3.5 (Debian 1:3.3.5-13)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from zope.schema import TextLine
>>> from zope.schema import Object
>>> from zope.interface import Interface, implements
First we define an Interface:
>>> class IPerson(Interface):
... first = TextLine(title=u"Firstname", required=False)
... last = TextLine(title=u"Lastname", required=True)
...
Then we write a class that implements it.
We use FieldProperty to check your attributes against our schema:
>>> from zope.schema.fieldproperty import FieldProperty
>>> class Person(object):
... implements(IPerson)
... first = FieldProperty(IPerson['first'])
... last = FieldProperty(IPerson['last'])
...
Now let's create an instance:
>>> k = Person()
>>> k.first = 'Franz'
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File
"/usr/local/src/Zope-3.2.0b2/build/lib.linux-i686-2.4/zope/schema/fieldproperty.py",
line 52, in __set__
field.validate(value)
File
"/usr/local/src/Zope-3.2.0b2/build/lib.linux-i686-2.4/zope/schema/_bootstrapfields.py",
line 138, in validate
self._validate(value)
File
"/usr/local/src/Zope-3.2.0b2/build/lib.linux-i686-2.4/zope/schema/_bootstrapfields.py",
line 263, in _validate
super(MinMaxLen, self)._validate(value)
File
"/usr/local/src/Zope-3.2.0b2/build/lib.linux-i686-2.4/zope/schema/_bootstrapfields.py",
line 165, in _validate
raise WrongType(value, self._type)
zope.schema._bootstrapinterfaces.WrongType: ('Franz', <type 'unicode'>)
It insists on unicode, so we give it unicode:
>>> k.first=u'Franz'
>>> k.last=u'Kafka'
We create a second person, but this time we leave the 'last' attribute
which is 'required' following our IPerson schema.
>>> f = Person()
>>> f.first = u'Felice'
Now we step forward to zope.schema.Object
Again we define an Interface and an implementing class:
>>> class ILetter(Interface):
... sender = Object(title=u"From", schema=IPerson)
... addressee = Object(title=u"To", schema=IPerson)
... date = TextLine(title=u"Date")
...
>>> class Letter(object):
... implements(ILetter)
... sender = FieldProperty(ILetter['sender'])
... addressee = FieldProperty(ILetter['addressee'])
... datum = FieldProperty(ILetter['date'])
...
>>> l = Letter()
Now let's see if zope.schema.Object works:
>>> l.sender = u'Kafka'
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File
"/usr/local/src/Zope-3.2.0b2/build/lib.linux-i686-2.4/zope/schema/fieldproperty.py",
line 52, in __set__
field.validate(value)
File
"/usr/local/src/Zope-3.2.0b2/build/lib.linux-i686-2.4/zope/schema/_bootstrapfields.py",
line 138, in validate
self._validate(value)
File
"/usr/local/src/Zope-3.2.0b2/build/lib.linux-i686-2.4/zope/schema/_field.py",
line 413, in _validate
raise SchemaNotProvided
zope.schema.interfaces.SchemaNotProvided
Okay, this was nice.
>>> l.sender = k
>>> l.sender
<__main__.Person object at 0xb7cdbeec>
Now let's have an other test:
>>> l.addressee = f
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File
"/usr/local/src/Zope-3.2.0b2/build/lib.linux-i686-2.4/zope/schema/fieldproperty.py",
line 52, in __set__
field.validate(value)
File
"/usr/local/src/Zope-3.2.0b2/build/lib.linux-i686-2.4/zope/schema/_bootstrapfields.py",
line 138, in validate
self._validate(value)
File
"/usr/local/src/Zope-3.2.0b2/build/lib.linux-i686-2.4/zope/schema/_field.py",
line 418, in _validate
raise WrongContainedType(errors)
zope.schema.interfaces.WrongContainedType: []
But we remember Felice's lastname:
>>> f.last = u'Bauer'
>>> l.addressee = f
Hey, check passes now. Everything seems ok with it.
Regards,
Christian
More information about the Zope3-users
mailing list