[Product-Developers] Dexterity: make a list field required

Sean Upton sdupton at gmail.com
Wed Dec 5 22:04:25 UTC 2012


On Wed, Dec 5, 2012 at 11:56 AM, Mikko Ohtamaa
<mikko+plone at redinnovation.com> wrote:
> Hi,
>
> On Wed, Dec 5, 2012 at 8:50 PM, Christian Ledermann
> <christian.ledermann at gmail.com> wrote:
>>
>> I try to make a list filed required for a dexterity type:
>>
>>
>>     layers = schema.List(
>>             title=_(u"Layers"),
>>             description=_(u"WMS Layers"),
>>             required=True,
>>             value_type=schema.Choice(
>>                  source=layers_vocab,
>>                  required=True,
>>                  ),
>>
>> but when I save the edit form it does not complain wether
>> the list field is filled or not
>
>
> My gut feeling is that it validates "list object" not "list length".
>
> Is there a validator which allows you to the require the minimum length of
> the list (probably not)?

Use a constraint, start with something that looks like:

>>> from zope import schema
>>> field = List(value_type=Int(), constraint=lambda v:len(v)>0, required=True)
>>> field.validate([1])
>>> field.validate([])
Traceback (most recent call last):
...
ConstraintNotSatisfied: []

I assume this should work, but have not tested with forms.

Sean


More information about the Product-Developers mailing list