Setting hidden fields with custom SQLFORMs

483 views
Skip to first unread message

davedigerati

unread,
Jul 29, 2013, 12:47:01 AM7/29/13
to web...@googlegroups.com
I've succeeded in using a custom SQLFORM to restrict how many fields of a table the user has to fill out, but now I am trying to set a hidden field value (status) and struggling.

I tried in the model to use:
    form = SQLFORM(db.games)
    form.vars.status = 'active'

but the field never gets written to the db, I believe because it is not one of the fields declared in html (below).

I then tried through the html as 
{{form.vars.status = 'active'}}
but while it was in the Accepted form.vars, it doesn't show up in request.vars and again, doesn't get written.

So, okay, let's place it in the html as 
{{request.vars.status = 'active'}}
And now it gets into request.vars, but again is not written to the db.
 
I think I've sunk over my head on this one, and advice correcting my approach above would be greatly appreciated.
Or was this a bad path to start down and I should just use a separate operation to write this field to the table after the record is created?
Thanks,
Dave

html if it helps:
{{=form.custom.begin}}
{{form.custom.widget.tm_home["_placeholder"] = "Home Team Name"}}
{{=form.custom.widget.tm_home}}
{{request.vars.status = "active"}}   <---  tried variations of this line & without it
{{=form.custom.submit}}
{{=form.custom.end}}

lesssugar

unread,
Jul 29, 2013, 7:30:17 PM7/29/13
to
Take a look at the book: http://web2py.com/books/default/chapter/29/07/forms-and-validators?search=hidden+fields#Hidden-fields

You can try defining your hidden field with hidden attribute:

SQLFORM(..., hidden=dict(hidden_field_name=hidden_field_value))

It should work.

davedigerati

unread,
Jul 29, 2013, 9:18:48 PM7/29/13
to web...@googlegroups.com
Well, thank you very much lesssugar, adding to my controller did indeed work, so at least i can move forward now;)
For anyone else, this is what worked:
    form = SQLFORM(db.games, hidden=dict(status="active"))

On Monday, July 29, 2013 7:23:30 PM UTC-4, lesssugar wrote:
Take a look at the book: http://web2py.com/books/default/chapter/29/07/forms-and-validators?search=hidden+fields#Hidden-fields

You can try defining your hidden field with hidden attribute:

SQLFORM(..., hidden=dict(hidden_field_name=hidden_field_value))

It should work.

On Monday, July 29, 2013 6:47:01 AM UTC+2, davedigerati wrote:

pumplerod

unread,
Mar 17, 2015, 1:38:18 PM3/17/15
to web...@googlegroups.com
Is there something else which needs to be setup to accomplish this?  I currently have...

db.define_table('brands',
   
Field('brand_name', type='string', requires=IS_NOT_EMPTY(),
          label
=T('Brand Name')),
   
Field('active', type='boolean',
          label
=T('active')))

form
= SQLFORM(db.brands,hidden=dict(active=True))


However when the form is displayed the field ('active') still shows up visible.



On Monday, July 29, 2013 at 6:23:30 PM UTC-5, lesssugar wrote:
Take a look at the book: http://web2py.com/books/default/chapter/29/07/forms-and-validators?search=hidden+fields#Hidden-fields

You can try defining your hidden field with hidden attribute:

SQLFORM(..., hidden=dict(hidden_field_name=hidden_field_value))

It should work.

On Monday, July 29, 2013 6:47:01 AM UTC+2, davedigerati wrote:

Dave S

unread,
Mar 17, 2015, 1:42:53 PM3/17/15
to web...@googlegroups.com


On Tuesday, March 17, 2015 at 10:38:18 AM UTC-7, pumplerod wrote:
Is there something else which needs to be setup to accomplish this?  I currently have...

db.define_table('brands',
   
Field('brand_name', type='string', requires=IS_NOT_EMPTY(),
          label
=T('Brand Name')),
   
Field('active', type='boolean',
          label
=T('active')))

form
= SQLFORM(db.brands,hidden=dict(active=True))


However when the form is displayed the field ('active') still shows up visible.


The book doesn't have a detailed example at the referenced section (although details of what it is translated to are shown).

Do you need quotes around the field name? 
hidden=dict("active"=True)
perhaps?

/dps



 

pumplerod

unread,
Mar 18, 2015, 3:11:44 PM3/18/15
to web...@googlegroups.com
I tried that.  It doesn't work either.  I've worked around my issue by simply not using hidden fields at all, but rather populating the fields after the form is submitted with the data I want recorded.

Diego Tostes

unread,
Sep 10, 2021, 2:59:33 PM9/10/21
to web2py-users
Is it possible to use on SQLFORM.grid?
Reply all
Reply to author
Forward
0 new messages