Re: [Zope3-Users] Formlib and invariants
Hi Florian, Yup. Thanks for that, much more elegant and passing in an error string displays nicely with error_views. Cheers, Darryl On Wed, 2006-07-19 at 22:18 +0200, Florian Lindner wrote: > Am Mittwoch, 19. Juli 2006 03:25 schrieb Darryl Cousins: > > Hi All, > > > > I've had a bit of a struggle getting formlib error views to play nicely > > with invariants. That is the problem I have found to trouble me is in > > the zope.formlib.form.FormBase method error_views. > > > > When I use the schema: > > > > class IMemberRegisterForm(IMemberData, IMemberDetails): > > """Schema for a member register form.""" > > > > new_password = Password( > > title=_("Choose a Password"), > > required=True) > > > > verify_password = Password( > > title=_("Verify Password"), > > required=True) > > > > @invariant > > def passwordsMatch(register): > > if register.new_password != register.verify_password: > > msg = _("Entered passwords do not match") > > error = ('verify_password', _("Passwords"), msg) > > raise Invalid(error) > > [...] > > I am not sure if I've understood you correctly, but I've solved the same > problem (raise error if passwords are not equal) this way: > > > class PasswordsAreNotEqual(ValidationError): > """The passwords are not equal.""" > interface.implements(IWidgetInputError) > > class IRegistrationForm(interface.Interface): > """For entering the data for registration.""" > > password = Password(title=u"Password", > description=u"Your password.", > required=True) > > password2 = Password(title=u"Verify Password", > required=True) > > @interface.invariant > def arePasswordsEqual(obj): > if obj.password != obj.password2: > raise PasswordsAreNotEqual > > > Hope this helps, > > Florian ___ Zope3-users mailing list Zope3-users@zope.org http://mail.zope.org/mailman/listinfo/zope3-users
Re: [Zope3-Users] Formlib and invariants
Am Mittwoch, 19. Juli 2006 03:25 schrieb Darryl Cousins: > Hi All, > > I've had a bit of a struggle getting formlib error views to play nicely > with invariants. That is the problem I have found to trouble me is in > the zope.formlib.form.FormBase method error_views. > > When I use the schema: > > class IMemberRegisterForm(IMemberData, IMemberDetails): > """Schema for a member register form.""" > > new_password = Password( > title=_("Choose a Password"), > required=True) > > verify_password = Password( > title=_("Verify Password"), > required=True) > > @invariant > def passwordsMatch(register): > if register.new_password != register.verify_password: > msg = _("Entered passwords do not match") > error = ('verify_password', _("Passwords"), msg) > raise Invalid(error) [...] I am not sure if I've understood you correctly, but I've solved the same problem (raise error if passwords are not equal) this way: class PasswordsAreNotEqual(ValidationError): """The passwords are not equal.""" interface.implements(IWidgetInputError) class IRegistrationForm(interface.Interface): """For entering the data for registration.""" password = Password(title=u"Password", description=u"Your password.", required=True) password2 = Password(title=u"Verify Password", required=True) @interface.invariant def arePasswordsEqual(obj): if obj.password != obj.password2: raise PasswordsAreNotEqual Hope this helps, Florian ___ Zope3-users mailing list Zope3-users@zope.org http://mail.zope.org/mailman/listinfo/zope3-users
[Zope3-Users] Formlib and invariants
Hi All, I've had a bit of a struggle getting formlib error views to play nicely with invariants. That is the problem I have found to trouble me is in the zope.formlib.form.FormBase method error_views. When I use the schema: class IMemberRegisterForm(IMemberData, IMemberDetails): """Schema for a member register form.""" new_password = Password( title=_("Choose a Password"), required=True) verify_password = Password( title=_("Verify Password"), required=True) @invariant def passwordsMatch(register): if register.new_password != register.verify_password: msg = _("Entered passwords do not match") error = ('verify_password', _("Passwords"), msg) raise Invalid(error) As far as I can ascertain I must raise the Invalid exception so that it is caught correctly in zope.interface.interface.InterfaceClass validateInvariants method. Now when the FormBase error_views method is called there is no multiadapter for the Invalid exception. First option then is to create a view for Invalid, I passed on that, and instead redefined error_views in my view class with the following: def error_views(self): for error in self.errors: if isinstance(error, basestring): yield error else: if not isinstance(error, WidgetInputError): e = list(error)[0] error = WidgetInputError(e[0], e[1], e[2]) if isinstance(error, WidgetInputError): view = getMultiAdapter( (error, self.request), IWidgetInputErrorView) title = getattr(error, 'widget_title', None) if title: yield '%s: %s' % (title, view.snippet()) else: yield view.snippet() There must be a cleaner way to do the same. That is to convert the Invalid error to a WidgetInputError. Has anyone done better? I am sure. Best regards, Darryl ___ Zope3-users mailing list Zope3-users@zope.org http://mail.zope.org/mailman/listinfo/zope3-users
Re: [Zope3-Users] Formlib and invariants?
On 05.05.2006, at 03:15, Gary Poster wrote: On May 4, 2006, at 7:44 AM, Bernd Dorn wrote: On 04.05.2006, at 13:35, Gary Poster wrote: On May 3, 2006, at 11:51 PM, Bernd Dorn wrote: yes, this should really be fixed up in the default implementation of formlib because interface.Invalid has no registered multiadapter to zope.app.form.browser.interfaces.IWidgetInputErrorView you have to raise a WidgetInputError an example for such an error: from zope.schema import Datetime,Bool,ValidationError from zope.app.form.interfaces import IWidgetInputError class FromGreaterThanTo(ValidationError): u"""From-Date is after To-Date""" # this is needed to adapt to a view for formlib implements(IWidgetInputError) Well, you can raise any error from an invariant that extends...zope.interface.Invalid, maybe? The invariant interface says what is allowed somewhere in zope.interface. ...I suppose we could have a generic view for Invalid errors that simply said "This is invalid, try again"? Doesn't seem very useful to me. You are responsible for making sure you have a reasonable exception view of an exception you raise. I'm inclined to think that this is a documentation bug at worst. yes, because in form.txt Invalid is used directly, wich implies that this should work Would you mind putting in a collector issue for this? http://www.zope.org/Collectors/Zope3-dev/599 Thanks Gary ___ Zope3-users mailing list Zope3-users@zope.org http://mail.zope.org/mailman/listinfo/zope3-users
Re: [Zope3-Users] Formlib and invariants?
On May 4, 2006, at 7:44 AM, Bernd Dorn wrote: On 04.05.2006, at 13:35, Gary Poster wrote: On May 3, 2006, at 11:51 PM, Bernd Dorn wrote: yes, this should really be fixed up in the default implementation of formlib because interface.Invalid has no registered multiadapter to zope.app.form.browser.interfaces.IWidgetInputErrorView you have to raise a WidgetInputError an example for such an error: from zope.schema import Datetime,Bool,ValidationError from zope.app.form.interfaces import IWidgetInputError class FromGreaterThanTo(ValidationError): u"""From-Date is after To-Date""" # this is needed to adapt to a view for formlib implements(IWidgetInputError) Well, you can raise any error from an invariant that extends...zope.interface.Invalid, maybe? The invariant interface says what is allowed somewhere in zope.interface. ...I suppose we could have a generic view for Invalid errors that simply said "This is invalid, try again"? Doesn't seem very useful to me. You are responsible for making sure you have a reasonable exception view of an exception you raise. I'm inclined to think that this is a documentation bug at worst. yes, because in form.txt Invalid is used directly, wich implies that this should work Would you mind putting in a collector issue for this? Thanks Gary ___ Zope3-users mailing list Zope3-users@zope.org http://mail.zope.org/mailman/listinfo/zope3-users
Re: [Zope3-Users] Formlib and invariants?
Bernd, That took care of it. Thanks a million. Mats On Thu, 4 May 2006 05:51:52 +0200, Bernd Dorn wrote > yes, this should really be fixed up in the default implementation of > formlib > > because interface.Invalid has no registered multiadapter to > zope.app.form.browser.interfaces.IWidgetInputErrorView > > you have to raise a WidgetInputError > > an example for such an error: > > from zope.schema import Datetime,Bool,ValidationError > from zope.app.form.interfaces import IWidgetInputError > > class FromGreaterThanTo(ValidationError): > u"""From-Date is after To-Date""" > > # this is needed to adapt to a view for formlib > implements(IWidgetInputError) > > On 03.05.2006, at 20:59, mats.nordgren wrote: > > > I'm trying to do a join form with formlib and are getting errors > > when checking > > invariants. > > > > I have my form interface IJoinForm > > > > class IJoinForm(Interface): > > """a join form""" > > > > username = schema.TextLine( > > title=_('User Name'), > > required=True) > > password = schema.Password( > > title=_('Password')) > > password_check = schema.Password( > > title=_('Enter Password Again')) > > title = schema.TextLine( > > title=_('Title')) > > > > @invariant > > def checkPasswords(obj): > > if obj.password != obj.password_check: > > raise Invalid("Passwords do not match") > > > > And then I have my implementation of form.Form: > > > > class JoinMetroSite(form.Form): > > form_fields = form.Fields(IJoinForm) > > > > @form.action('Join') > > def handle_join_action(self, action, data): > > principal_folder = zapi.getUtility(IAuthenticatorPlugin, > > 'principal') > > principal = InternalPrincipal(data['username'], data > > ['password'], > > data['title']) > > principal_folder[data['username']] = principal > > > > My form in registered with: > > > > > for="*" > > class=".metrositeforms.JoinMetroSite" > > name="join.html" > > permission="zope.View" > > menu="zmi_views" title="Join" /> > > > > When I try the join.html form I get the following error: > > > > Traceback (innermost last): > > Module zope.publisher.publish, line 138, in publish > > result = publication.callObject(request, object) > > Module zope.app.publication.zopepublication, line 161, in callObject > > return mapply(ob, request.getPositionalArguments(), request) > > Module zope.publisher.publish, line 113, in mapply > > return debug_call(object, args) > >- __traceback_info__: > zope.app.publisher.browser.viewmeta.JoinMetroSite instance at > > 0x034C9B50> > > Module zope.publisher.publish, line 119, in debug_call > > return object(*args) > > Module zope.formlib.form, line 739, in __call__ > > return self.render() > > Module zope.formlib.form, line 733, in render > > self.form_result = self.template() > > Module zope.app.pagetemplate.viewpagetemplatefile, line 83, in > > __call__ > > return self.im_func(im_self, *args, **kw) > > Module zope.app.pagetemplate.viewpagetemplatefile, line 51, in > > __call__ > > sourceAnnotations=getattr(debug_flags, 'sourceAnnotations', 0), > > Module zope.pagetemplate.pagetemplate, line 117, in pt_render > > strictinsert=0, sourceAnnotations=sourceAnnotations)() > > Module zope.tal.talinterpreter, line 277, in __call__ > > self.interpret(self.program) > > Module zope.tal.talinterpreter, line 352, in interpret > > handlers[opcode](self, args) > > Module zope.tal.talinterpreter, line 878, in do_defineMacro > > self.interpret(macro) > > Module zope.tal.talinterpreter, line 352, in interpret > > handlers[opcode](self, args) > > Module zope.tal.talinterpreter, line 926, in do_extendMacro > > definingName, extending) > > Module zope.tal.talinterpreter, line 908, in do_useMacro > > self.interpret(macro) > > Module zope.tal.talinterpreter, line 352, in interpret > > handlers[opcode](self, args) > > Module zope.tal.talinterpreter, line 538, in do_optTag_tal > > self.do_optTag(stuff) > > Module zope.tal.talinterpreter, line 523, in do_optTag > > return self.no_tag(start, program) > > Module zope.tal.talinterpreter, line 518, in no_tag > > self.interpret(program) > > Module zope.tal.talinterpreter, line 352, in interpret > > handlers[opcode](self, args) > > Module zope.tal.talinterpreter, line 878, in do_defineMacro > > self.interpret(macro) > > Module zope.tal.talinterpreter, line 352, in interpret > > handlers[opcode](self, args) > > Module zope.tal.talinterpreter, line 976, in do_defineSlot > > self.interpret(block) > > Module zope.tal.talinterpreter, line 352, in interpret > > handlers[opcode](self, args) > > Module zope.tal.talinterpreter, line 966, in do_defineSlot > > self.interpret(slot) > > Module zope.tal.talinterpreter, line 352, in interpr
Re: [Zope3-Users] Formlib and invariants?
On 04.05.2006, at 13:35, Gary Poster wrote: On May 3, 2006, at 11:51 PM, Bernd Dorn wrote: yes, this should really be fixed up in the default implementation of formlib because interface.Invalid has no registered multiadapter to zope.app.form.browser.interfaces.IWidgetInputErrorView you have to raise a WidgetInputError an example for such an error: from zope.schema import Datetime,Bool,ValidationError from zope.app.form.interfaces import IWidgetInputError class FromGreaterThanTo(ValidationError): u"""From-Date is after To-Date""" # this is needed to adapt to a view for formlib implements(IWidgetInputError) Well, you can raise any error from an invariant that extends...zope.interface.Invalid, maybe? The invariant interface says what is allowed somewhere in zope.interface. ...I suppose we could have a generic view for Invalid errors that simply said "This is invalid, try again"? Doesn't seem very useful to me. You are responsible for making sure you have a reasonable exception view of an exception you raise. I'm inclined to think that this is a documentation bug at worst. yes, because in form.txt Invalid is used directly, wich implies that this should work Gary ___ Zope3-users mailing list Zope3-users@zope.org http://mail.zope.org/mailman/listinfo/zope3-users
Re: [Zope3-Users] Formlib and invariants?
On May 3, 2006, at 11:51 PM, Bernd Dorn wrote: yes, this should really be fixed up in the default implementation of formlib because interface.Invalid has no registered multiadapter to zope.app.form.browser.interfaces.IWidgetInputErrorView you have to raise a WidgetInputError an example for such an error: from zope.schema import Datetime,Bool,ValidationError from zope.app.form.interfaces import IWidgetInputError class FromGreaterThanTo(ValidationError): u"""From-Date is after To-Date""" # this is needed to adapt to a view for formlib implements(IWidgetInputError) Well, you can raise any error from an invariant that extends...zope.interface.Invalid, maybe? The invariant interface says what is allowed somewhere in zope.interface. ...I suppose we could have a generic view for Invalid errors that simply said "This is invalid, try again"? Doesn't seem very useful to me. You are responsible for making sure you have a reasonable exception view of an exception you raise. I'm inclined to think that this is a documentation bug at worst. Gary ___ Zope3-users mailing list Zope3-users@zope.org http://mail.zope.org/mailman/listinfo/zope3-users
Re: [Zope3-Users] Formlib and invariants?
yes, this should really be fixed up in the default implementation of formlib because interface.Invalid has no registered multiadapter to zope.app.form.browser.interfaces.IWidgetInputErrorView you have to raise a WidgetInputError an example for such an error: from zope.schema import Datetime,Bool,ValidationError from zope.app.form.interfaces import IWidgetInputError class FromGreaterThanTo(ValidationError): u"""From-Date is after To-Date""" # this is needed to adapt to a view for formlib implements(IWidgetInputError) On 03.05.2006, at 20:59, mats.nordgren wrote: I'm trying to do a join form with formlib and are getting errors when checking invariants. I have my form interface IJoinForm class IJoinForm(Interface): """a join form""" username = schema.TextLine( title=_('User Name'), required=True) password = schema.Password( title=_('Password')) password_check = schema.Password( title=_('Enter Password Again')) title = schema.TextLine( title=_('Title')) @invariant def checkPasswords(obj): if obj.password != obj.password_check: raise Invalid("Passwords do not match") And then I have my implementation of form.Form: class JoinMetroSite(form.Form): form_fields = form.Fields(IJoinForm) @form.action('Join') def handle_join_action(self, action, data): principal_folder = zapi.getUtility(IAuthenticatorPlugin, 'principal') principal = InternalPrincipal(data['username'], data ['password'], data['title']) principal_folder[data['username']] = principal My form in registered with: When I try the join.html form I get the following error: Traceback (innermost last): Module zope.publisher.publish, line 138, in publish result = publication.callObject(request, object) Module zope.app.publication.zopepublication, line 161, in callObject return mapply(ob, request.getPositionalArguments(), request) Module zope.publisher.publish, line 113, in mapply return debug_call(object, args) - __traceback_info__: zope.app.publisher.browser.viewmeta.JoinMetroSite instance at 0x034C9B50> Module zope.publisher.publish, line 119, in debug_call return object(*args) Module zope.formlib.form, line 739, in __call__ return self.render() Module zope.formlib.form, line 733, in render self.form_result = self.template() Module zope.app.pagetemplate.viewpagetemplatefile, line 83, in __call__ return self.im_func(im_self, *args, **kw) Module zope.app.pagetemplate.viewpagetemplatefile, line 51, in __call__ sourceAnnotations=getattr(debug_flags, 'sourceAnnotations', 0), Module zope.pagetemplate.pagetemplate, line 117, in pt_render strictinsert=0, sourceAnnotations=sourceAnnotations)() Module zope.tal.talinterpreter, line 277, in __call__ self.interpret(self.program) Module zope.tal.talinterpreter, line 352, in interpret handlers[opcode](self, args) Module zope.tal.talinterpreter, line 878, in do_defineMacro self.interpret(macro) Module zope.tal.talinterpreter, line 352, in interpret handlers[opcode](self, args) Module zope.tal.talinterpreter, line 926, in do_extendMacro definingName, extending) Module zope.tal.talinterpreter, line 908, in do_useMacro self.interpret(macro) Module zope.tal.talinterpreter, line 352, in interpret handlers[opcode](self, args) Module zope.tal.talinterpreter, line 538, in do_optTag_tal self.do_optTag(stuff) Module zope.tal.talinterpreter, line 523, in do_optTag return self.no_tag(start, program) Module zope.tal.talinterpreter, line 518, in no_tag self.interpret(program) Module zope.tal.talinterpreter, line 352, in interpret handlers[opcode](self, args) Module zope.tal.talinterpreter, line 878, in do_defineMacro self.interpret(macro) Module zope.tal.talinterpreter, line 352, in interpret handlers[opcode](self, args) Module zope.tal.talinterpreter, line 976, in do_defineSlot self.interpret(block) Module zope.tal.talinterpreter, line 352, in interpret handlers[opcode](self, args) Module zope.tal.talinterpreter, line 966, in do_defineSlot self.interpret(slot) Module zope.tal.talinterpreter, line 352, in interpret handlers[opcode](self, args) Module zope.tal.talinterpreter, line 878, in do_defineMacro self.interpret(macro) Module zope.tal.talinterpreter, line 352, in interpret handlers[opcode](self, args) Module zope.tal.talinterpreter, line 878, in do_defineMacro self.interpret(macro) Module zope.tal.talinterpreter, line 352, in interpret handlers[opcode](self, args) Module zope.tal.talinterpreter, line 976, in do_defineSlot self.interpret(block) Module zope.tal.talinterpreter, line 352, in interpret handlers[opcode](self, args) Module zope.tal.talinterpreter, line 878, in do_defineMacro self.interpret(macro) Module zope.tal.talinterpreter, line 352, in interpret
[Zope3-Users] Formlib and invariants?
I'm trying to do a join form with formlib and are getting errors when checking invariants. I have my form interface IJoinForm class IJoinForm(Interface): """a join form""" username = schema.TextLine( title=_('User Name'), required=True) password = schema.Password( title=_('Password')) password_check = schema.Password( title=_('Enter Password Again')) title = schema.TextLine( title=_('Title')) @invariant def checkPasswords(obj): if obj.password != obj.password_check: raise Invalid("Passwords do not match") And then I have my implementation of form.Form: class JoinMetroSite(form.Form): form_fields = form.Fields(IJoinForm) @form.action('Join') def handle_join_action(self, action, data): principal_folder = zapi.getUtility(IAuthenticatorPlugin, 'principal') principal = InternalPrincipal(data['username'], data['password'], data['title']) principal_folder[data['username']] = principal My form in registered with: When I try the join.html form I get the following error: Traceback (innermost last): Module zope.publisher.publish, line 138, in publish result = publication.callObject(request, object) Module zope.app.publication.zopepublication, line 161, in callObject return mapply(ob, request.getPositionalArguments(), request) Module zope.publisher.publish, line 113, in mapply return debug_call(object, args) - __traceback_info__: Module zope.publisher.publish, line 119, in debug_call return object(*args) Module zope.formlib.form, line 739, in __call__ return self.render() Module zope.formlib.form, line 733, in render self.form_result = self.template() Module zope.app.pagetemplate.viewpagetemplatefile, line 83, in __call__ return self.im_func(im_self, *args, **kw) Module zope.app.pagetemplate.viewpagetemplatefile, line 51, in __call__ sourceAnnotations=getattr(debug_flags, 'sourceAnnotations', 0), Module zope.pagetemplate.pagetemplate, line 117, in pt_render strictinsert=0, sourceAnnotations=sourceAnnotations)() Module zope.tal.talinterpreter, line 277, in __call__ self.interpret(self.program) Module zope.tal.talinterpreter, line 352, in interpret handlers[opcode](self, args) Module zope.tal.talinterpreter, line 878, in do_defineMacro self.interpret(macro) Module zope.tal.talinterpreter, line 352, in interpret handlers[opcode](self, args) Module zope.tal.talinterpreter, line 926, in do_extendMacro definingName, extending) Module zope.tal.talinterpreter, line 908, in do_useMacro self.interpret(macro) Module zope.tal.talinterpreter, line 352, in interpret handlers[opcode](self, args) Module zope.tal.talinterpreter, line 538, in do_optTag_tal self.do_optTag(stuff) Module zope.tal.talinterpreter, line 523, in do_optTag return self.no_tag(start, program) Module zope.tal.talinterpreter, line 518, in no_tag self.interpret(program) Module zope.tal.talinterpreter, line 352, in interpret handlers[opcode](self, args) Module zope.tal.talinterpreter, line 878, in do_defineMacro self.interpret(macro) Module zope.tal.talinterpreter, line 352, in interpret handlers[opcode](self, args) Module zope.tal.talinterpreter, line 976, in do_defineSlot self.interpret(block) Module zope.tal.talinterpreter, line 352, in interpret handlers[opcode](self, args) Module zope.tal.talinterpreter, line 966, in do_defineSlot self.interpret(slot) Module zope.tal.talinterpreter, line 352, in interpret handlers[opcode](self, args) Module zope.tal.talinterpreter, line 878, in do_defineMacro self.interpret(macro) Module zope.tal.talinterpreter, line 352, in interpret handlers[opcode](self, args) Module zope.tal.talinterpreter, line 878, in do_defineMacro self.interpret(macro) Module zope.tal.talinterpreter, line 352, in interpret handlers[opcode](self, args) Module zope.tal.talinterpreter, line 976, in do_defineSlot self.interpret(block) Module zope.tal.talinterpreter, line 352, in interpret handlers[opcode](self, args) Module zope.tal.talinterpreter, line 878, in do_defineMacro self.interpret(macro) Module zope.tal.talinterpreter, line 352, in interpret handlers[opcode](self, args) Module zope.tal.talinterpreter, line 538, in do_optTag_tal self.do_optTag(stuff) Module zope.tal.talinterpreter, line 523, in do_optTag return self.no_tag(start, program) Module zope.tal.talinterpreter, line 518, in no_tag self.interpret(program) Module zope.tal.talinterpreter, line 352, in interpret handlers[opcode](self, args) Module zope.tal.talinterpreter, line 872, in do_condition self.interpret(block) Module zope.tal.talinterpreter, line 352, in interpret handlers[opcode](self, args) Module zope.tal.talinterpreter, line 872, in do_condition self.interpret(block) Module zope.tal.talin