Re: [Zope3-dev] Add flexibility to zope.formlib.form.EditFormBase

2007-05-05 Thread Marius Gedminas
On Fri, May 04, 2007 at 09:26:17AM +0200, Michael Howitz wrote:
> I'm using zope.formlib.form.EditFormBase and want to normalize data  
> before saving it.
> Saving is done by the applyChanges function which is called from  
> handle_edit_action.
> The problem is that handle_edit_action is an action, so I can't  
> easily subclass and do a super call to handle_edit_action.

You can, but there's a trick to it---call handle_edit_action.success()
instead of calling handle_edit_action directly.

Marius Gedminas
-- 
Remember the... the... uhh.


signature.asc
Description: Digital signature
___
Zope3-dev mailing list
Zope3-dev@zope.org
Unsub: http://mail.zope.org/mailman/options/zope3-dev/archive%40mail-archive.com



Re: [Zope3-dev] Add flexibility to zope.formlib.form.EditFormBase

2007-05-04 Thread Maciej Wisniowski

>
> No, my suggestion does not break any existing code because it only
> adds a new method to EditFormBase which gets called from
> handle_edit_action instead of the function. This new method then calls
> the applyChanges function. So you are able to overwrite the new method
> in a subclass to normalize before applying.
OK, I understand now.

>
> -1
> A method called "validate" should only tell valid or not (and why) but
> it should not change the data.
Currently when defining action you can use 'validator', which is in fact
called by 'validate' function
in handleSubmit:

@action('Edit', validator=my_validator)
def edit(self, **data):
...

As Fred Drake said:
"The validator is responsible for populating `data` with the valid
values.  That's definitely covered in the docs somewhere. "

There was some discussion about that in zope3-users list:
http://mail.zope.org/pipermail/zope3-users/2007-March/005934.html

So I think you're right about the name (which in fact is 'validator', I
misspelled this a bit)
but I think that current state is even worse because 'validator' is not
for validation in fact
but for populating with 'valid' data.

The real problem is where we can do validations (if it is not possible
to use invariants)
and where we can modify data just like in your case. I think that
function like 'validator',
maybe called 'prepare_data' or something, that would get 'data'
dictionary filled with values
would be really helpful.
Also few simple changes to ease validation and setting errors for
widgets would be helpful.
I've seen that Grok will bring some changes to formlib, but I don't know
exactly what will
be different. AFAIR applyChanges will be changed to different name.

-- 
Maciej Wisniowski

___
Zope3-dev mailing list
Zope3-dev@zope.org
Unsub: http://mail.zope.org/mailman/options/zope3-dev/archive%40mail-archive.com



Re: [Zope3-dev] Add flexibility to zope.formlib.form.EditFormBase

2007-05-04 Thread Michael Howitz

Am 04.05.2007 um 10:28 schrieb Maciej Wisniowski:
[...]

What code you have to copy-paste from formlib when you add your own
handle_edit_action?
I think it is common to do something like:

from zope.formlib import form
form.applyChanges(...)


The method EditFormBase.handle_edit_action contains code to set the  
status message which I have to copy-paste when I want to use it  
unchanged.



My solution would be to add an applyChanges method to
zope.formlib.form.EditFormBase which calls the applyChanges function.
Any objections?

Possibly a lot of existing code uses form.applyChanges as in example
above so this may be a compatibility problem.


No, my suggestion does not break any existing code because it only  
adds a new method to EditFormBase which gets called from  
handle_edit_action instead of the function. This new method then  
calls the applyChanges function. So you are able to overwrite the new  
method in a subclass to normalize before applying.


[...]

For me it would be better to change handleSubmit method
and the way action.validate is called there.

[...]

This way validate gets 'data' parameter that already has
values from widgets, these values may be simply validated and/or
normalized. There is even no need to change edit action.


-1
A method called "validate" should only tell valid or not (and why)  
but it should not change the data.


Yours sincerely,
Michael Howitz

gocept gmbh & co. kg · forsterstrasse 29 · 06112 halle/saale
www.gocept.com · fon: +49 345 12298898 · fax: +49 345 12298891


___
Zope3-dev mailing list
Zope3-dev@zope.org
Unsub: http://mail.zope.org/mailman/options/zope3-dev/archive%40mail-archive.com



Re: [Zope3-dev] Add flexibility to zope.formlib.form.EditFormBase

2007-05-04 Thread Maciej Wisniowski

> I'm using zope.formlib.form.EditFormBase and want to normalize data
> before saving it.
> Saving is done by the applyChanges function which is called from
> handle_edit_action.
> The problem is that handle_edit_action is an action, so I can't easily
> subclass and do a super call to handle_edit_action.
> If I add my own handle_edit_action I have to copy-paste the code from
> formlib because applyChanges is a function, not a method on the class.
What code you have to copy-paste from formlib when you add your own
handle_edit_action?
I think it is common to do something like:

from zope.formlib import form
form.applyChanges(...)

>
> My solution would be to add an applyChanges method to
> zope.formlib.form.EditFormBase which calls the applyChanges function.
> Any objections?
Possibly a lot of existing code uses form.applyChanges as in example
above so this may be a compatibility problem.

For me it would be better to change handleSubmit method
and the way action.validate is called there.

Currently action.validate method can be used to add some values to
'data' dictionary but not to simply validate form values because 'data'
parameter is an empty dictionary when 'validate' is called.
I'd like to have action.validate called with 'data' dictionary that is
already filled with values.

I mean that handleSubmit function may be changed to something like:

def handleSubmit(actions, data, default_validate=None):
for action in actions:
if action.submitted():
#  default_validate is called before
action.validate --
errors = default_validate(action, data)
if errors is None:
errors = action.validate(data)
return errors, action

return None, None

This way validate gets 'data' parameter that already has
values from widgets, these values may be simply validated and/or
normalized. There is even no need to change edit action.

-- 
Maciej Wisniowski
___
Zope3-dev mailing list
Zope3-dev@zope.org
Unsub: http://mail.zope.org/mailman/options/zope3-dev/archive%40mail-archive.com



[Zope3-dev] Add flexibility to zope.formlib.form.EditFormBase

2007-05-04 Thread Michael Howitz

Hi,

I'm using zope.formlib.form.EditFormBase and want to normalize data  
before saving it.
Saving is done by the applyChanges function which is called from  
handle_edit_action.
The problem is that handle_edit_action is an action, so I can't  
easily subclass and do a super call to handle_edit_action.
If I add my own handle_edit_action I have to copy-paste the code from  
formlib because applyChanges is a function, not a method on the class.


My solution would be to add an applyChanges method to  
zope.formlib.form.EditFormBase which calls the applyChanges function.

Any objections?

Yours sincerely,
Michael Howitz

gocept gmbh & co. kg · forsterstrasse 29 · 06112 halle/saale
www.gocept.com · fon: +49 345 12298898 · fax: +49 345 12298891


___
Zope3-dev mailing list
Zope3-dev@zope.org
Unsub: http://mail.zope.org/mailman/options/zope3-dev/archive%40mail-archive.com