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=uPassword, description=uYour password., required=True) password2 = Password(title=uVerify 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
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=uPassword, description=uYour password., required=True) password2 = Password(title=uVerify 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?
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): uFrom-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: page 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__: security proxied 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
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): uFrom-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
[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: page 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__: security proxied 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 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