On Nov 14, 10:42 am, Alberto Valverde <[EMAIL PROTECTED]> wrote:
> On Nov 14, 2006, at 1:49 PM, miya wrote:
>
>
>
>
>
> > Is there another way to validate a TableForm Widget without
> > declaring/defining it structure outside the Root class (or any class
> > for that matter) ?
>
> > -------------------------
>
> > For example:
>
> > (Fromhttp://docs.turbogears.org/1.0/ErrorHandling)
>
> > from turbogears import controllers, expose, redirect
> > from turbogears import validate, validators as v
> > from turbogears import error_handler
> > from turbogears import widgets as w
>
> > number_form = w.ListForm(
> >     fields = [
> >         w.TextField(
> >             name="number",
> >             label="Enter a Number",
> >             validator=v.Int(not_empty=True))
> >     ],
> >     submit_text = "Submit Number!"
> > )
>
> > ## This is what the template looks like
> > # <html xmlns:py="http://purl.org/kid/ns#";>
> > #     <body>
> > #         <h1>This is a Form Page!</h1>
> > #         ${form(value_of('data',None), action=action, method="POST")}
> > #     </body>
> > # </html>
>
> > class Root(controllers.RootController):
>
> >     @expose(template=".templates.welcome")
> >     def index(self,number=-1,tg_errors=None):
> >         return dict(data={'number':number},
> >                     form=number_form,
> >                     action="/validated_number")
>
> >     @expose()
> >     @error_handler(index)
> >     @validate(form=number_form)
> >     def validated_number(self, number=2):
> >         return dict(valid_number=number)
>
> > -------------------------
>
> > Here number_form is declared/defined outside the Root class. Wouldn't
> > it be great if its defined inside the index method? I know that if
> > I do
> > that, the @validate decorator tag can't find the number_form. So, is
> > there another way to do this? Because I just can't imagine to declare
> > and define every and each one of my forms outside the Root (or any )
> > class. Or is this the correct/standard way to do this?
> Yep, that's the correct way to do it. If you need to pass a form
> dynamically to @validate you can pass a callable that returns the
> form when called without args. However, for 'index' to redisplay
> previous input and errors when called by errorhandling owhen
> validation fails, exactly the same form instance that validated input
> must be the one that redisplays errors ("displayed_form is
> validated_form").
>
> Why would make so great that the form was defined inside "index"?
> Keep in mind that the form would have to be initialized on every
> request instead of just once on "compile" time... If you need to
> reuse fields for similar but different forms you can define common
> fields in a WidgetsLists (or a plain ol' python list) and concatenate
> them as needed.
>
> Alberto

It seems and feels weird to treat forms in such a special way.
May be its because I come from the php/smarty world.

Anyway, thanks a lot!!

--
miya


--~--~---------~--~----~------------~-------~--~----~
 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
-~----------~----~----~----~------~----~------~--~---

Reply via email to