[Zope3-Users] Should I be using a more basic form in Formlib than EditForm?

Stephan Richter srichter at cosmos.phy.tufts.edu
Mon Jan 8 02:20:25 EST 2007


On Saturday 30 December 2006 06:37, Adam Summers wrote:
> I have an edit form:
> class claimEditForm(form.EditForm):
>     form_fields = form.Fields(Iclaim).omit('__name__', '__parent__')
>
>     @form.action(_("MyApply"), condition=form.haveInputWidgets)  
>     def handle_edit_action(self, action, data):
>         if self.context.modify(data):
>             self.status = _(u"Object Updated")
>         else:
>             self.status = _(u"No Changes")
>
> The object has a modify method, which looks like this:
>     def modify(self, kw):
>         fieldnames = getFieldNames(Iclaim)
>         for k in kw.keys():
>             if k not in fieldnames:
>                 raise Invalid("Invalid Field to set: %s", k)
>             else:
>                 field = Iclaim[k]
>                 field.validate(kw[k])
>                 setattr(self, k, kw[k])
>         self._completeValues() ###This method calculates some missing
> data for the object (if possible) or raises an error
>         Iclaim.validateInvariants(self)
>         return True

Ugh, why don't you just use zope.schema.fieldproperty.FieldProperty and do 
this in your view code:

for name, value in data:
    setattr(self.context, name, value)

Note that data should not contain fields that do not exist in the class.

> The thing I don't understand is this: The invariant method I have in the
> interface is being called twice.
>
> First Pass through, object passed to my invariant is of type:
>     FormData: <zope.formlib.form.FormData instance at 0x3f07c10>
>
> Call stack is:
>     debug_call [publish.py:114]  
>     __call__ [form.py:773]  
>     update [form.py:740]  
>     handleSubmit [form.py:683]  
>     validate [form.py:720]  
>     checkInvariants [form.py:504]  
>     validateInvariants [interface.py:583]  
>     claimInvariants [interfaces.py:39]
>
> (In certain cases the data will be invalid, because I need to prep a few
> fields via claim._completeValues() )
>
> Second Pass through, object passed to my invariant function is of type:
>     claim: <as.claim.claim.claim object at 0x3591770>
>
> Call stack is:
>     debug_call [publish.py:114]    
>     __call__ [form.py:773]    
>     update [form.py:754]    
>     success [form.py:598]    
>     handle_edit_action [forms.py:23] (This is my action being called
> now!)    
>     modify [claim.py:42] (At this point in the code, the
> as.claim.claim.claim object has been modified with the contents of the
> form)    
>     validateInvariants [interface.py:583]    
>     claimInvariants [interfaces.py:39]    
>  
>
> My question is: In order to not have the variants checked before my data
> is prepped, should I really be using a form.EditForm as the base, or
> some other class?

Invariants are checked by formlib. See zope.formlib.form, line 721. So all you 
need to do is override the validate() method.

Regards,
Stephan
-- 
Stephan Richter
CBU Physics & Chemistry (B.S.) / Tufts Physics (Ph.D. student)
Web2k - Web Software Design, Development and Training


More information about the Zope3-users mailing list