Re: [Zope3-Users] Re: Formlib - handleSubmit, custom validator on action
Cheers all. On Thu, 2007-03-29 at 09:24 -0400, Fred Drake wrote: > On 3/29/07, Darryl Cousins <[EMAIL PROTECTED]> wrote: > > OK. Then what I'm missing is how to assign a value to that variable > > `data` because in my code > ... > > So I am always getting an empty dictionary back to my action method. > > Don't set the `data` variable; add values to it: > > data["myfield"] = somevalue > > > -Fred > ___ Zope3-users mailing list Zope3-users@zope.org http://mail.zope.org/mailman/listinfo/zope3-users
Re: [Zope3-Users] Re: Formlib - handleSubmit, custom validator on action
On 3/29/07, Darryl Cousins <[EMAIL PROTECTED]> wrote: OK. Then what I'm missing is how to assign a value to that variable `data` because in my code ... So I am always getting an empty dictionary back to my action method. Don't set the `data` variable; add values to it: data["myfield"] = somevalue -Fred -- Fred L. Drake, Jr. "Every sin is the result of a collaboration." --Lucius Annaeus Seneca ___ Zope3-users mailing list Zope3-users@zope.org http://mail.zope.org/mailman/listinfo/zope3-users
[Zope3-Users] Re: Formlib - handleSubmit, custom validator on action
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 [EMAIL PROTECTED]> 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 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. ___ Zope3-users mailing list Zope3-users@zope.org http://mail.zope.org/mailman/listinfo/zope3-users
Re: [Zope3-Users] Re: Formlib - handleSubmit, custom validator on action
> So I am always getting an empty dictionary back to my action method. > > Sorry for the noise. In the meantime I've just gone back to creating an > interface with and @interface.invariant which is working for me. I was > just keen to find out if validator could be used. > I used validation directly in action function especially because I had some rdbms operations here. I've written a bit about this recently. Search for 'Formlib and custom validation constraints' thread. It is something like: Function defined in my view class eg. descendant of EditForm from zope.app.form.interfaces import WidgetInputError def setWidgetError(self, name, v): """ Helper function for validation purposes. Sets error for widget @name - field name @v - error message """ w = self.widgets.get(name) if w: w._error = WidgetInputError( self.context.__name__, w.label, v) return w._error In function used as success handler I did: if not data['myfield']: error = self.setWidgetError('myfield', 'Required field is empty') self.errors.append(error) self.form_reset = False -- Maciej Wisniowski ___ Zope3-users mailing list Zope3-users@zope.org http://mail.zope.org/mailman/listinfo/zope3-users
[Zope3-Users] Re: Formlib - handleSubmit, custom validator on action
Hi, 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 <[EMAIL PROTECTED]> 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 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 >> {'blah':'forgive me'} and a print statement in formlib/form.py directly after line 736: errors, action = handleSubmit(self.actions, data, self.validate) print data >> {} So I am always getting an empty dictionary back to my action method. Sorry for the noise. In the meantime I've just gone back to creating an interface with and @interface.invariant which is working for me. I was just keen to find out if validator could be used. Regards, Darryl ___ Zope3-users mailing list Zope3-users@zope.org http://mail.zope.org/mailman/listinfo/zope3-users
[Zope3-Users] Re: Formlib - handleSubmit, custom validator on action
Darryl Cousins wrote: On Thu, 2007-03-29 at 05:52 -0400, Fred Drake wrote: On 3/29/07, Darryl Cousins <[EMAIL PROTECTED]> 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 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. -- http://worldcookery.com -- Professional Zope documentation and training ___ Zope3-users mailing list Zope3-users@zope.org http://mail.zope.org/mailman/listinfo/zope3-users