You're right, either method would work.  But is it possible without having 
to look up the record twice (once when creating the form, the other to 
validate it)?  

Another option would be to pass the amount (record.total) as a hidden field 
rather than retrieve it using the record_id....

-Jim 

On Monday, July 15, 2013 6:11:52 AM UTC-5, viniciusban wrote:
>
> Yes, there are some solutions and you gave one of them: create the 
> record id as a hidden field in your form. 
>
> Other solution can be using request.args(0) in your _validate_form() 
> function. 
>
> On Mon, Jul 15, 2013 at 8:01 AM, Jim Gregory 
> <[email protected]<javascript:>> 
> wrote: 
> > I have a form with five text input boxes: 
> > 
> > form = FORM(*[INPUT('amount-%s' % i, _type='number') for i in range(5)], 
> > INPUT(_type='submit')) 
> > 
> > The sum of these input boxes must equal the value of a field in a 
> database 
> > record. 
> > 
> > I would like to pass the database record as a parameter to the 
> onvalidation 
> > callback of the form, but as I understand it, this is not possible--the 
> > onvalidation function can only be passed one argument, the function 
> itself. 
> > 
> > So, instead, I have written a second function to check validation after 
> the 
> > form passes its initial validation: 
> > 
> > def _validate_form(form, record): 
> >     total = 0.0 
> >     for f in request.vars: 
> >         if request.vars[f].startswith('amount-') and request.vars[f] != 
> '': 
> >             total += float(request.vars[f]) 
> >     if total != record.total: 
> >     for f in request.vars: 
> >         if request.vars[f].startswith('amount-') and request.vars[f] != 
> '': 
> >             form.errors[f] = 'total of amounts must equal record total' 
> >     return form.errors 
> > 
> > def test(): 
> >     record = db.table[request.args(0)] 
> >     form = FORM(*[INPUT('amount-%s' % i, _type='number') for i in 
> > range(5)],INPUT(_type='submit')) 
> >     if form.validate(): 
> >         form.errors = _validate_form(form, record) 
> >         if not form.errors: 
> >             redirect('next_page') 
> >     return form 
> > 
> > The trouble I am having is the test--'form.errors' returns True (and 
> hence 
> > 'not form.errors' returns False) even when _validate_form returns an 
> empty 
> > form.errors Storage object. 
> > 
> > I don't understand why it does.  How should I test for this condition 
> > instead? 
> > 
> > Is there another way I could do this using the onvalidation function, 
> other 
> > than passing the record id as a hidden form field and doing the record 
> > selection in the validation function?  (In my actual application, the 
> record 
> > must be selected before the form is created.) 
> > 
> > -- 
> > 
> > --- 
> > You received this message because you are subscribed to the Google 
> Groups 
> > "web2py-users" group. 
> > To unsubscribe from this group and stop receiving emails from it, send 
> an 
> > email to [email protected] <javascript:>. 
> > For more options, visit https://groups.google.com/groups/opt_out. 
> > 
> > 
>

-- 

--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to