On Mar 26, 9:22 pm, Brian Will <[email protected]> wrote:
> Found my own answer: I can create an onvalidation function for the
> form that only runs when the form is accepted but before the new
> record is made.

Right

> The problem this then introduces is how to set the
> enclosing variable from the function without use of nonlocal.

you could add it to the form

> Guess I
> can just create a dict for the vars ahead of time and set that from
> the function. Like so:
>
>     vars = {}
>
>     def get_uuid(form):
>         from uuid import uuid4
>         uuid = uuid4()
>         db.job_post.uuid.default = lambda: uuid
>         vars['uuid'] = uuid

def get_uuid(form):
    from uuid import uuid4
    form.vars.uuid = uuid4()

Setting a field's default value is useful before creating a form,
at this point you are between acceptance and db i/o.
BTW, you don't need lambda to set a default.

>     if form.accepts(request.vars, session, onvalidation=get_uuid):
>         vars['poster_name'] = form.vars.poster_name
>         vars['poster_email'] = form.vars.poster_email
>         redirect(URL('post_email', vars=vars))

if form.accepts(request.vars, session, onvalidation=get_uuid):
   redirect(URL('post_email',
       vars={'poster_name' : form.vars.poster_name,
             'poster_email' : form.vars.poster_email})

Reply via email to