On 25/04/2006, at 3:52, Roger Rohrbach wrote:
> It turns out that the method bodies can be the same; the only
> difference is the form--the two objects have different attributes,
> validation requirements etc. I want something like:
>
> class Add(controllers.Root):
> ...
>
> @validate(form=???)
> def saveElement(type):
> # do what's needed to save an object of type 'type'
>
> You see my dilemma: the decorator needs to know which form to
> associate
> with the method, but that depends on the method's 'type' argument. I
> can't, for example, call a function:
>
> @validate(form=getForm('type'))
>
> since 'type' is not defined when the decorator is applied. I can't
> use
> a callable:
>
> @validate(form=getForm)
>
> because the callable really needs a 'type' argument to get passed at
> runtime.
>
> Am I just completely missing the point here? Surely there's a way to
> associate the correct form with my method at runtime?
I think the only way you can do this with the current implementation
is to validate the values yourself inside the controller:
def controllermethod(self, **kw):
form = get_form("type")
try:
kw = form.validate(kw)
# good, coerced values in kw
except Invalid, e:
tg_errors = e.unpack_errors()
# do something with errors
Or you can factor the common "do something with good values" part
into an unexposed method and split your exposed method in various
differently-decorated exposed methods, one for each form...
HTH,
Alberto
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"TurboGears" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/turbogears
-~----------~----~----~----~------~----~------~--~---