[Zope-dev] z3c.form - extraction from sequence widget

Martin Aspeli optilude at gmx.net
Thu Aug 28 17:53:38 EDT 2008


Stephan Richter wrote:
> On Thursday 28 August 2008, Martin Aspeli wrote:
>> I suspect that we just need widget.SequenceWidget in z3c.form to have a
>> special case for len(self.terms)==1, i.e. it's a checkbox with only one
>> option, or maybe a special case for a BooleanField, or indeed have
>> different widgets for Boolean and ICollection fields.
> 
> Widgets should not make these sort of decisions. I think this is the 
> responsibility of the data converter. I am pretty sure that writing more 
> specific data converters will solve our problem.

In the implementation of extract() in the SequenceWidget(), the 
extract() method does this:

     def extract(self, default=interfaces.NOVALUE):
         """See z3c.form.interfaces.IWidget."""
         if (self.name not in self.request and
             self.name+'-empty-marker' in self.request):
             return []
         value = self.request.get(self.name, default)
         if value != default:
             for token in value:
                 if token == self.noValueToken:
                     continue
                 try:
                     self.terms.getTermByToken(token)
                 except LookupError:
                     return default
         return value


It's the 'return []' that's the problem, i.e. it's treating the return 
value when empty-marker is in place as something other than the 
'default' that the base widget class will use when the field isn't in 
the request:

     def extract(self, default=interfaces.NOVALUE):
         """See z3c.form.interfaces.IWidget."""
         return self.request.get(self.name, default)

I think to get those same semantics for sequence fields, we'll need 
something in the extract() method.

I also don't quite understand why we use [] and not NOVALUE, even when 
the field isn't required. It seems to me that the BooleanField case is 
the special case. In fact, it's somewhat weird that a BooleanField, 
which is scalar, should default to using a SequenceWidget.

Martin

-- 
Author of `Professional Plone Development`, a book for developers who
want to work with Plone. See http://martinaspeli.net/plone-book



More information about the Zope-Dev mailing list