I am using web2py forms and i want to have some fields only visible to user (as fixed which cannot be edited). I tried making various combinations of editable, writeable, readonly but was of no use. I looked into web2py book too but that also seems insufficient. It would be great if someone can tell me how to do this.
Asked
Active
Viewed 2,646 times
3
-
1Can you show your code? If you're using SQLFORM or Crud forms based on a db table, you should be able to set db.tablename.fieldname.writable=False (though you'll have to do that sometime before the form is created). If you're using the FORM helper or just creating forms manually in HTML, then you could add the "readonly" attribute to the input tag (though this can be hacked by an attacker), or just display a value outside of an input tag. – Anthony Jul 06 '11 at 14:41
1 Answers
2
You mean some fields visible to all visitors and some fields visible only if logged in?
If that's the case, then build your form conditionally:
form_fields = [
Field('pubfield'),
Field('pubfield2')
]
if auth.user: # This is true if the end-user is logged in and you're using the built-in auth
form_fields.append(Field('private_field'))
return dict(form=FORM(form_fields))
Unless you're not talking about logged in users, and just want to make the fields be visible, but not editable. Then, use writable=False like you tried, but I think you have to either use crud.create/crud.update or SQLFORM / SQLFORM.factory (the latter does not require a data model)
SQLFORM.factory(Field('my_readable_field', writable=False))
If you the form is based off of a database, you can use CRUD (you'll need to modify the settings for CRUD if you're not using authentication, so that CRUD forms are accessible)
crud.create(db.some_table)
or
SQLFORM(db.some_table)

Kasapo
- 5,294
- 1
- 19
- 21