Yes- it looks very bad. I think the below method will work. (Let me know if you need any elaboration)rofields = [db.<table>.field1, .....]hidden = {}for f in rofields:fname = str(f).split('.')[1]hidden[fname] = r[fname]form1 = SQLFORM(db.<table>, record=<recid> , showid=True, hidden=hidden)
[form1.element(_name=f.name).update(_disabled=True) for f in rofields]
for field in db.mytable:
if field.readable and not field.writable:
field.writable = True # So the widget will be used to display the value.
field.widget = lambda f, v: SQLFORM.widgets[field.type].widget(f, v, _readonly=True)
form = SQLFORM(db.mytable, record).process()
On Tuesday, November 29, 2016 at 8:52:07 AM UTC-5, Sundar wrote:Yes- it looks very bad. I think the below method will work. (Let me know if you need any elaboration)rofields = [db.<table>.field1, .....]hidden = {}for f in rofields:fname = str(f).split('.')[1]hidden[fname] = r[fname]form1 = SQLFORM(db.<table>, record=<recid> , showid=True, hidden=hidden)
Note, to get the name of a field, just do field.name. Also, not sure why the hidden fields (SQLFORM won't do anything with their contents), and I don't see "r" defined anywhere.
Some simpler code to disable the form fields:
[form1.element(_name=f.name).update(_disabled=True) for f in rofields]
An alternative approach is to change the widgets associated with read-only fields:
for field in db.mytable:
if field.readable and not field.writable:
field.widget = lambda f, v: SQLFORM.widgets[field.type].widget(f, v, _disabled=True)
form = SQLFORM(db.mytable, record).process()
Anthony
2. hidden is included to make sure that the update back end operation does not make those fields as NULL. Not sure if the below alternatives will work: (a) using 'readonly' attribute so that the values go to the back end - but does it happen in all the browsers? (b) may be we can remove the disabled attribute just before submitting the form?)
It takes some javascript to make Anthony's solution work:
[form1.element(_name=f.name).update(_disabled=True) for f in rofields]
disabled fields won't pass form validation without some js code in the corresponding view:
<script>
jQuery(document).ready(function() {
$( "[class|='form']" ).submit(function(){
$( ".form-control[disabled]" ).prop( "disabled", false );
});
});
</script>
It takes some javascript to make Anthony's solution work:
[form1.element(_name=f.name).update(_disabled=True) for f in rofields]
the readonly prop doesn't prevent users from "messing-up" with the readonly fields and to change their values (reference fields) at list in the form: not very clean user interface in my opinion......:(
The readonly attribute is a boolean attribute.
When present, it specifies that an input field or textarea is read-only.
A read-only field cannot be modified (however, a user can tab to it, highlight it, and copy the text from it).
The readonly attribute can be set to keep a user from changing the value until some other conditions have been met (like selecting a checkbox, etc.). Then, a JavaScript can remove the readonly value, and make the input field editable.
The readonly attribute can be used on the following elements:
Elements | Attribute |
---|---|
<input> | readonly |
<textarea> | readonly |
sorry to insist Anthony
but the select tags (used for reference fields) are still editable with readonly="readonly"
your first option with _disabled works for me.....alignment is perfect and it's safe....
When you need to place plain text form next to a label within at form, use the .form-control-static class on a <p>.
label.readonly ~ div {padding-top: 7px;}