If you only need it in one controller, put it in the controller.

On May 18, 10:36 am, scausten <[email protected]> wrote:
> I think I've found the answer in onvalidate, but would like some
> clarification. In the manual, is the followiong code:
>
> db.define_table('numbers',
>     Field('a', 'integer'),
>     Field('b', 'integer'),
>     Field('c', 'integer', readable=False, writable=False))
>
> def my_form_processing(form):
>     c = form.vars.a * form.vars.b
>     if c < 0:
>        form.errors.b = 'a*b cannot be negative'
>     else:
>        form.vars.c = c
>
> def insert_numbers():
>    form = SQLFORM(db.numbers)
>    if form.accepts(request.vars, session,
>                    onvalidation=my_form_processing)
>        session.flash = 'record inserted'
>        redirect(request.url)
>    return dict(form=form)
>
> Does the 'my_form_processing' function go in the model or the
> controller?
>
> On May 18, 2:09 pm, scausten <[email protected]> wrote:
>
> > I have a file uploads table and want to have a unique, random code for
> > each file as it is uploaded, so that my fields are id, file, code,
> > where 'code' is generated by a given function (say, for sake of
> > argument, randint(100000,999999)).
>
> > Is this something I can do in the model, or do I need a form to call
> > the function as the form is submitted?

Reply via email to