had you try widgets?e.g.widget_date_disable = lambda field, value: \SQLFORM.widgets.date.widget(field, value,_disable = True,_class = "date form-control")
def sqlform2(*args, **kwargs):
table = kwargs.get('table', args[0])
fields = kwargs.get('fields', [f for f in table])
readonly_fields = [f for f in fields if not f.writable]
[setattr(f, 'writable', True) for f in readonly_fields]
form = SQLFORM(*args, **kwargs)
form.readonly_fields = [f.name for f in readonly_fields]
[form.custom.widget[f.name].update(_readonly=True, requires=None) for f in readonly_fields]
[setattr(f, 'writable', False) for f in readonly_fields]
return form
def remove_readonly_vars(form):
[form.vars.pop(f) for f in form.readonly_fields]
def myform():
form = sqlform2(db.mytable, request.args(0)).process(onvalidation=remove_readonly_vars)
return dict(form=form)
We should probably make what you are doing a built-in option (maybe even the default), but for now, you can try something like this:
def sqlform2(*args, **kwargs):
table = kwargs.get('table', args[0])
fields = kwargs.get('fields', [f for f in table])
readonly_fields = [f for f in fields if not f.writable]
[setattr(f, 'writable', True) for f in readonly_fields]
form = SQLFORM(*args, **kwargs)
[form.custom.widget[f.name].update(_readonly=True) for f in readonly_fields]
[setattr(f, 'writable', False) for f in readonly_fields]
return form
def myform():
form = sqlform2(db.mytable, request.args(0)).process()
return dict(form=form)
The above sets the readonly fields to writable before creating the form, and then sets the _readonly HTML attribute of the widgets after creating the form (you could also set _disabled=True if desired). It then resets all the readonly fields so they are not writable before returning, so when the form is processed, those fields will not be included in the database write.
We should probably make what you are doing a built-in option (maybe even the default), but for now, you can try something like this:
def sqlform2(*args, **kwargs):
table = kwargs.get('table', args[0])
fields = kwargs.get('fields', [f for f in table])
readonly_fields = [f for f in fields if not f.writable]
[setattr(f, 'writable', True) for f in readonly_fields]
form = SQLFORM(*args, **kwargs)
[form.custom.widget[f.name].update(_readonly=True) for f in readonly_fields]
[setattr(f, 'writable', False) for f in readonly_fields]
return form
def myform():
form = sqlform2(db.mytable, request.args(0)).process()
return dict(form=form)
The above sets the readonly fields to writable before creating the form, and then sets the _readonly HTML attribute of the widgets after creating the form (you could also set _disabled=True if desired). It then resets all the readonly fields so they are not writable before returning, so when the form is processed, those fields will not be included in the database write.
[form.custom.widget[f.name].update(_readonly=True, requires=[]) for f in readonly_fields]
We should probably make what you are doing a built-in option (maybe even the default), but for now, you can try something like this:
def sqlform2(*args, **kwargs):
table = kwargs.get('table', args[0])
fields = kwargs.get('fields', [f for f in table])
readonly_fields = [f for f in fields if not f.writable]
[setattr(f, 'writable', True) for f in readonly_fields]
form = SQLFORM(*args, **kwargs)
[form.custom.widget[f.name].update(_readonly=True) for f in readonly_fields]
[setattr(f, 'writable', False) for f in readonly_fields]
return form
def myform():
form = sqlform2(db.mytable, request.args(0)).process()
return dict(form=form)
Anthony --One more tiny but not insignificant detail...I found I had to add "requires=[]" to the custom.widget:
[form.custom.widget[f.name].update(_readonly=True, requires=[]) for f in readonly_fields]
Otherwise, the field keeps the 'requires' of the original Field and the check fails. The above solution is working very well!
form.element('input', _id='RentalTransactionMaster_TotalAmt')['_readonly']=''
Placed in controller after the form = SQLFORM(...) declaration.