Thank you very much.
But seems following works for me too:

    form = SQLFORM.factory(
        Field('find_contact_string', label="Choose from contacts"),
        Field('contact_id', readable=False, writable=False),
        hidden=dict(contact_id='')
        )
    if form.process(onvalidation=_validate_form).accepted:
        db.invoice[request.args(0)] = dict(contact_id=form.vars.contact_id)
        redirect(URL(......))        
    return dict(form=form)

def _validate_form(form):
    if request.vars.contact_id:
        form.vars.contact_id = request.vars.contact_id
    else:
        form.errors.find_contact_string = "Enter a text to find the 
customer"

I assign the errors message to the visible field (find_contact_string).
contact_id I change with jQuery:
$('[name=contact_id]').val(2);





Dne neděle, 9. února 2014 17:26:27 UTC+1 A36_Marty napsal(a):
>
> A little more progress -- hopefully to help someone else that Googles for 
> this and ends up stumped like I was...  I've spent an inordinate amount of 
> time and frustration figuring out little nuances that I wouldn't wish upon 
> anyone...   
>
> Gotca's that I learned:
>
> 1) To hide a field, first remove it from the fields to be shown in the 
> SQLFORM, then add it back with the hidden= attribute.  Just specifying it 
> as hidden does not remove it.
>
> i.e. This does NOT work, the field 'hidden' is shown to the user:
> db.define_table('person',
>                Field('name', 'string'),
>                Field('hidden', 'string'))
>
> def index():
>    form = SQLFORM(db.person, hidden=dict(hidden="Default hidden text"))
>
>     form.vars.hidden = request.vars.
>    
>
> This DOES work:
>
> db.define_table('person',
>               Field('name', 'string'),
>               Field('hidden', 'string'))
>
> def index():
>   form = SQLFORM(db.person, fields=['name'], hidden=dict(hidden="Default 
> hidden text"))
>
>     form.vars.hidden = request.vars.
>
>
> 2) To get a list of fields to show (from a larger table definition, so as 
> to not hand-type each field name), you must get a copy of the fields from 
> the table object, not just a reference to the fields.
>
> i.e. This DOES NOT work.  The field will be be removed and unseen by the 
> user, however since Python works by reference, the field is removed from 
> the table definition and the form.accepted() will fail as web2py will try 
> to insert a value into field that no longer exists.
>
> db.define_table('person',
>               Field('name', 'string'),
>               Field('hidden', 'string'))
>
> def index():
>
>   fields_to_show = db.person.fields
>   fields_to_show.remove('hidden')
>
>   form = SQLFORM(db.person, fields= fields_to_show, 
> hidden=dict(hidden="Default 
> hidden text"))
>
>
>
>     form.vars.hidden = request.vars.Enter code here...
>
> This DOES work (only 3 characters difference) - making a copy of the 
> fields list instead of getting it by reference:
>
> db.define_table('person',
>               Field('name', 'string'),
>               Field('hidden', 'string'))
>
> def index():
>
>   fields_to_show = db.person.fields*[:]*
>   fields_to_show.remove('hidden')
>
>   form = SQLFORM(db.person, fields= fields_to_show, 
> hidden=dict(hidden="Default 
> hidden text"))
>
>
>
>     form.vars.hidden = request.vars.Enter code here...
>
>
> 3. To update the value of the hidden fields (or any fields), the form.vars 
> modification must be before the form.process call.
>
> i.e. This DOES NOT work.
>
> db.define_table('person',
>                 Field('name', 'string'),
>                 Field('hidden', 'string'))
>
> def index():
>     form = SQLFORM(db.person, fields=['name'], hidden=dict(hidden="Default 
> hidden text"))
>
>     form.vars.hidden = request.vars.
>     
>     if form.process(onvalidation=validate_form).accepted:
>         pass
>
>     return dict(form=form)
>
> def validate_form(form):
>
>     *form.vars.hidden = "Changed hidden text"   #Never gets written to 
> database*
>     form.accepted = True
>
> This does work:
>
> db.define_table('person',
>                 Field('name', 'string'),
>                 Field('hidden', 'string'))
>
> def index():
>     form = SQLFORM(db.person, fields=['name'], hidden=dict(hidden="Default 
> hidden text"))
>
>     form.vars.hidden = request.vars.
>
>     *form.vars.hidden = "Changed hidden text"   #Successfully written to 
> database*
>     
>     if form.process(onvalidation=validate_form).accepted:
>         pass
>
>     return dict(form=form)
>
> def validate_form(form):
>
>     form.accepted = True
>
> Take care...
>
>
>

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
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/d/optout.

Reply via email to