Re: [Zope3-Users] Invariants and forms

2007-08-13 Thread Hermann Himmelbauer
Am Montag, 13. August 2007 12:01 schrieb Darryl Cousins:
 Hi,

 This was answered recently me thinks.

 http://mail.zope.org/pipermail/zope3-users/2007-August/006648.html

I think this covers a z3c.form-related issue, but not formlib...

Best Regards,
Hermann

-- 
[EMAIL PROTECTED]
GPG key ID: 299893C7 (on keyservers)
FP: 0124 2584 8809 EF2A DBF9  4902 64B4 D16B 2998 93C7
___
Zope3-users mailing list
Zope3-users@zope.org
http://mail.zope.org/mailman/listinfo/zope3-users


Re: [Zope3-Users] Invariants and forms

2007-08-13 Thread Darryl Cousins
Hi,

This was answered recently me thinks.

http://mail.zope.org/pipermail/zope3-users/2007-August/006648.html

Regards,
Darryl

On Mon, 2007-08-13 at 11:37 +0200, [EMAIL PROTECTED] wrote:
 Hello, I'm using an invariant to check if given passwords are equal.
 
 I've found two different examples(see below), but both don't display my given 
 exception message. The only thing i get is the standard error message.
 
 Since this is my first experience with zope, could someone explain to me the 
 correct way to get a custom error message with invariants?
 
 Thanks!!!
 I'm using zope version 3.3.1.
 
 First example
 (taken from: 
 http://blog.gocept.com/zope3-testen-von-felderuebergreifenden-bedingungen-in-interfaces-mit-zope-formlib/):
 
 class PasswordsAreNotEqual(zope.schema.ValidationError):
 uDas Passwort und die Wiederholung sind nicht gleich.
 zope.interface.implements(zope.app.form.interfaces.IWidgetInputError)
 
 def arePasswordsEqual(obj)
 if obj.password != obj.password2:
 raise PasswordsAreNotEqual
 
 from zope.interface import Interface, invariant
 
 class IUser(Interface):
 password = zope.schema.Password(title=uPasswort)
 password2 = zope.schema.Password(title=uWiederholung des Passworts)
 
 arePasswordsEqual = invariant(arePasswordsEqual)
 
 
 Second example
 (taken from:
 http://blog.gocept.com/zope3-testen-von-felderuebergreifenden-bedingungen-invariants-in-interfaces-mit-zope-formlib-aktualisiert)
 
 import zope.interface
 
 class IUser(zope.interface.Interface):
 password = zope.schema.Password(title=uPasswort)
 password2 = zope.schema.Password(title=uWiederholung des Passworts)
 
 @zope.interface.invariant
 def arePasswordsEqual(user):
 if user.password != user.password2:
 raise zope.interface.Invalid(
 uDas Passwort und die Wiederholung sind nicht gleich.)

___
Zope3-users mailing list
Zope3-users@zope.org
http://mail.zope.org/mailman/listinfo/zope3-users


Re: [Zope3-Users] Invariants and forms

2007-08-13 Thread Stephan Richter
On Monday 13 August 2007 05:37, [EMAIL PROTECTED] wrote:
 Since this is my first experience with zope, could someone explain to me
 the correct way to get a custom error message with invariants?

I am not sure how or whether formlib does it these days, but raising 
Invalid('string') should work. If not, it probably would require fixing 
formlib or writing a lot of code to override existing components.

Regards,
Stephan
-- 
Stephan Richter
CBU Physics  Chemistry (B.S.) / Tufts Physics (Ph.D. student)
Web2k - Web Software Design, Development and Training
___
Zope3-users mailing list
Zope3-users@zope.org
http://mail.zope.org/mailman/listinfo/zope3-users


Re: [Zope3-Users] Invariants and forms

2007-08-13 Thread Maciej Wisniowski
 I've found two different examples(see below), but both don't display my given 
 exception message. The only thing i get is the standard error message.
What do you mean by standard error message?


 Second example
 (taken from:
 http://blog.gocept.com/zope3-testen-von-felderuebergreifenden-bedingungen-invariants-in-interfaces-mit-zope-formlib-aktualisiert)
 
 import zope.interface
 
 class IUser(zope.interface.Interface):
 password = zope.schema.Password(title=uPasswort)
 password2 = zope.schema.Password(title=uWiederholung des Passworts)
 
 @zope.interface.invariant
 def arePasswordsEqual(user):
 if user.password != user.password2:
 raise zope.interface.Invalid(
 uDas Passwort und die Wiederholung sind nicht gleich.)

I think this should work.
Take a look at formlib/form.txt  - section about Invariants. If
it doesn't then I think your error is from somewhere else (again - what
is standard error message for you?)

There is also another possibility to do some checks between distinct
fields. To do this, in your action handler you may do your check
using values from 'data' dictionary, and if there is error you should
set:

self.form_reset = True
self.errors.append(u'your error message')


If you want to use this kind of validation (at action handler) to set
error messages for specific field you may do this either. I've added
function to my Form class for this:


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 this case, if you validate your values in action handler you may do:


self.setWidgetError('password_confirmation',
u'Confirmation doesn\'t match')
self.form_reset = True


and error message will be bound to 'password_confirmation' field.


-- 
Maciej Wisniowski
___
Zope3-users mailing list
Zope3-users@zope.org
http://mail.zope.org/mailman/listinfo/zope3-users