miya wrote:
> On Nov 14, 11:58 am, "Aaron Bostick" <[EMAIL PROTECTED]>
> wrote:
> > Miya,
> >
> > It can be done like this:
> >
> > if tg_errors:
> > cherrypy.request.validated_form = form
> >
> > Just put the above in your error handling controller method, which
> > should be the same method that defines and creates the form.
> >
> > Regards,
> > Aaron
>
> Hi Aaron! I tried what you posted, but it doesn't work. I don't know
> what to do with de @validate tag of the "action method". May be I'm
> doing sth. wrong...
> Here is the code...
>
> //
> ------------------------------------------------------------------------------------
>
> import logging
>
> import cherrypy
>
> import turbogears
> from turbogears import controllers, expose, validate, redirect
> from turbogears import identity
> from turbogears import widgets, validators as v, error_handler
>
> from intranet import json
>
> from intranet.models.agenda.User import User as dbUser
>
> log = logging.getLogger("intranet.controllers")
>
>
> class userNameExists(v.UnicodeString):
> def validate_python(self, user_name, state = None):
> try:
> dbUser.by_user_name(user_name)
> raise v.Invalid("El usuario ya existe" , user_name,
> state)
> except LookupError:
> pass
>
> class RegisterFields(widgets.WidgetsList):
> user_name = widgets.TextField(
> name = 'user_name',
> label = 'Nombre',
> attrs={'size': 32, 'maxlength': 255},
> validator=v.All(v.NotEmpty, userNameExists))
> user_surname = widgets.TextField(
> name = 'user_surname',
> label = 'Apellido',
> attrs={'size': 32, 'maxlength': 255},
> validator=v.All(v.NotEmpty, v.UnicodeString ))
> email_address = widgets.TextField(
> name='email_address', label='Correo
> electronico',
> attrs={'size': 32, 'maxlength': 64},
> validator=v.All(v.NotEmpty, v.Email))
> display_name = widgets.TextField(
> name = 'display_name',
> label = 'Nick',
> attrs={'size': 32, 'maxlength': 255},
> validator=v.All(v.NotEmpty, v.UnicodeString))
> password = widgets.PasswordField(
> name = 'password',
> label = 'Contrasena',
> attrs={'size': 32, 'maxlength': 255},
> validator=v.All(v.NotEmpty, v.UnicodeString ))
> passwordDuplicate = widgets.PasswordField(
> name = 'passwordDuplicate',
> label = 'Verificacion contrasena',
> attrs={'size': 32, 'maxlength': 255},
> validator=v.All(v.NotEmpty, v.UnicodeString))
> submit_text = "Ingresar usuario"
>
>
>
> class User(controllers.Controller):
>
> @expose(template='intranet.templates.agenda.insertUser')
> def insertUser(self, tg_errors = None):
> if tg_errors:
> cherrypy.request.validated_form = insert_user_form
>
> insert_user_form= widgets.TableForm(fields=RegisterFields(),
> validator =
> v.Schema(chained_validators=[v.FieldsMatch("password",
> "passwordDuplicate")]))
> return dict(user_form = insert_user_form,
> submit_text = 'Ingresar usuario',
> method = 'post',
> action = 'insertado')
> @expose()
> def index(self):
> return '''
> <h1>hola</h1>
> '''
>
> @error_handler(insertUser)
> @validate(form = insert_user_form) <------- What do I do with this?
> @expose()
> def insertado(self,user_name, email_address, display_name,
> user_surname, password, passwordDuplicate,tg_errors = None):
> dbUser(user_name = user_name,
> email_address = email_address,
> display_name = display_name,
> user_surname = user_surname,
> password = password)
>
> return '''
> <h1>user inserted</h1>
> '''
>
> @expose()
> def default(self):
> return '''
> <h1>hola</h1>
> '''
> //
> ------------------------------------------------------------------------------------
>
> If I comment the @validate tag, of course it doesn't validate...( lol )
> Sooo... I'm kinda lost with your advice. What should I do?
>
> Thanks a lot.
"insert_user_form" is local to "insertUser" so validate won't be able
to "see" it. If you really want to define the form inside the
controller method you should declare "insert_user_form" as being
global, however, you could... errr, wil run into thread-safety issues
if you do that.
Really, forms (and widgets in general) were designed to be declared
just once outside any controller method and be reused in any controller
that needs them... they're stateless (hence thread-safe) so there's no
risk in doing that (unless you change a mutable attribute, which you
shouldn't).
Sorry for being so repetitive but... what exactly frightens you about
instantiating the form outside your controller? ;) From the use-case
you're showing in your code there's nothing preventing you from doing
it that way (unless I'm missing something).
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?hl=en
-~----------~----~----~----~------~----~------~--~---