Specify default values for custom form

44 views
Skip to first unread message

F.C.

unread,
Mar 19, 2018, 10:13:28 PM3/19/18
to web2py-users
I use a custom form within my application to capture comments for specific records. The user clicks on a record and is directed to another view where they can capture the comments.

Is it possible to define a custom value for a specific custom form field, and then hide that field so that the user does not see it. The reason I need to do that is that I want to save the comment to the associated record against whom the comments should be recorded.

I hope this provides enough guidance on what I am looking for.


Dave S

unread,
Mar 20, 2018, 1:30:04 AM3/20/18
to web2py-users

I think there's help in the book:

  • writable declares whether a field is writable in forms.
  • readable declares whether a field is readable in forms. If a field is neither readable nor writable, it will not be displayed in create and update forms.
found at
<URL:http://web2py.com/books/default/chapter/29/06/the-database-abstraction-layer#Field-constructor>

(warning: the online copy of the book still refers to "CRUD" as new; it is now deprecated)

Another reference that might be useful is
<URL:http://web2py.com/books/default/chapter/29/07/forms-and-validators#Conditional-fields>
 
/dps

Dave S

unread,
Mar 20, 2018, 1:43:16 AM3/20/18
to web2py-users

Also, you can turn the readable and writable setting on or off on the fly:

<URL:http://web2py.com/books/default/chapter/29/06/the-database-abstraction-layer#Run-time-field-and-table-modification>

/dps
 

Dave S

unread,
Mar 21, 2018, 4:24:33 AM3/21/18
to web2py-users


On Monday, March 19, 2018 at 10:43:16 PM UTC-7, Dave S wrote:
[...] 
Also, you can turn the readable and writable setting on or off on the fly:

<URL:http://web2py.com/books/default/chapter/29/06/the-database-abstraction-layer#Run-time-field-and-table-modification>

I have a couple examples on my home development machine 

 def func1():
    db
.run.distance.readable = False
    db
.run.duration.readable = False
    form
= SQLFORM(db.run)
   
[...]


def func2():
   
if request.vars.startseg:
     
try:
        db
.segment.isstart.default = (request.vars.startseg == 'T' )
       
print "isstart " + ('True' if db.segment.isstart.default  else "False")
        db
.segment.isstart.readable = db.segment.isstart.default
       
print "isstart should be readable now %d" % (db.segment.isstart.readable)
     
except ValueError:
       
pass
   
else:
        db
.segment.isstart.writable = False
       
print "isstart should not be readable now %d" % (db.segment.isstart.writable)
    db
.segment.isstart.writable = db.segment.isstart.default
   
print "isstart should not be writable now %d" % (db.segment.isstart.writable)
    form
= SQLFORM(db.segment)
   
[...]


I have yet to try using custom forms, although they pop up in threads from time to time.

/dps

Reply all
Reply to author
Forward
0 new messages