[Zope3-Users] Re: Formlib - handleSubmit, custom validator on action

Philipp von Weitershausen philipp at weitershausen.de
Thu Mar 29 09:22:21 EDT 2007


On 29 Mar 2007, at 15:11 , Darryl Cousins wrote:
> On Thu, 2007-03-29 at 14:44 +0200, Philipp von Weitershausen wrote:
>> Darryl Cousins wrote:
>>> On Thu, 2007-03-29 at 05:52 -0400, Fred Drake wrote:
>>>> On 3/29/07, Darryl Cousins <darryl- 
>>>> dF9uhgPY3yxHRB3IUnr2w72Sh7SDDEB6 at public.gmane.org> wrote:
>>>>> but the line previous says
>>>>>
>>>>>   data = {}
>>>>>
>>>>> So my validator always receives an empty dictionary to validate.
>>>> The validator is responsible for populating `data` with the valid
>>>> values.  That's definitely covered in the docs somewhere.
>>>>
>>>>
>>>>   -Fred
>>>>
>>>
>>> Cheers for the reply Fred. Indeed form.txt in formlib does say:
>>>
>>> """
>>> If the validator is provided as a method name, the method  will be
>>> called with the action and a dictionary in which to save data.
>>> """
>>>
>>> Then the assumption is that the custom validator method will get the
>>> submitted values from the form object (also passed to the method  
>>> along
>>> with the action and `empty` dictionary, though that isn't  
>>> mentioned in
>>> form.txt). I'm thinking that the use-case for this functionality has
>>> been lost in development; historical flotsam. Who, after all,  
>>> needs an
>>> empty dictionary passed to a method? And the method is expected to
>>> return <quote form.txt> a (usually empty) list of widget input  
>>> errors.
>>> So what is the point of having an empty dict to populate?
>>>
>>> `data` itself is not returned, nor available outside the method,  
>>> so your
>>> answer "with the valid values" makes little sense to me. But  
>>> maybe I'm
>>> missing something?
>>
>> You are. The validator is not given just *any* empty dictionary,  
>> it is
>> given the data dictionary that will later be passed to the action.
>>
>>
>
> OK. Then what I'm missing is how to assign a value to that variable
> `data` because in my code
>
> def my_validator(form, action, data):
> 	data = {'blah':'forgive me'}
> 	print data

Dude, you really need to learn about Python object identities and  
references... What you're doing is creating a *new* dictionary  
(that's what the {} literal does in Python) and assigning that to a  
local scope variable. The 'data' object that's passed in as a  
parameter is no longer referenced. You want to say::

   data['blah'] = 'forgive me'.

If you're familiar with C, think of every variable in Python as a  
pointer to the actual, real object.


More information about the Zope3-users mailing list