krsyoung schrieb:
> Hey,
>
> I'm trying to save myself some code by applying multiple forms to the
> same save function. Basically I have a simple and complex form that
> can both be represented by the same underlying database object. The
> simple form has a subset of the fields that the complex form has.
> What I am trying to do is figure out a way so that the @validate
> statement knows if it should be calling the simple or complex form for
> validation. For example:
>
> def get_form(self, type=None):
>
> if type == None or type == 'simple':
> return simple_form
> else:
> return complex_form
>
> Then down in my save function I have:
>
> @expose()
> @validate(form=get_form('???'))
> @error_handler(request)
> def save(self, **kwargs):
>
> Is there anyway to update the '???' at runtime? Is this a good path
> to be going down or should I suck it up and have a copy & paste party?
It's a good path, but you misunderstan something about callbacks in
validate.
You do *not* call get_form in the validate, but pass it as reference:
@validate(form=get_form)
Then in get_form, you can e.g. inspect the request to have a look at a
hidden form value you pass for distinction:
def get_form():
if cherrypy.request.params.get('formtype') == "complex":
return complex_form
return simple_form
The above code is untested, and has two uncertainties:
- I don't know if that's the way to get to the actual request params
- but something like that for sure works.
- I don't know if get_form get's passed anything - if in doubt,
capture with *args or such.
Diez
--~--~---------~--~----~------------~-------~--~----~
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?hl=en
-~----------~----~----~----~------~----~------~--~---