for t in db.tables:
for f in db[t].fields:
if db[t][f].required:
db[t][f].comment = SCRIPT('jQuery("#%s_%s").attr("required","")' % (t,f))
def add_required(elm):
elm['_class'] += ' required'
db.mytable.myfield.onrender = add_required
Ideally, I wouldn't set the class via a script, that was just a hack. I would like to alter the field after its default widget is rendered. I have a patch that allows you to do this. Thoughts?:def add_required(elm):
elm['_class'] += ' required'
db.mytable.myfield.onrender = add_required
Anthony
Check out http://dev.s-cubism.com/plugin_notemptymarker.Anthony
def my_string_widget(field, value):
return INPUT(_name=field.name, _id="%s_%s" % (field._tablename, field.name),
_class="whatever",
_value=value,
_required=""
requires=field.requires)
Field('comment', 'string', widget=my_string_widget)
that enables you to do all sorts of crazy things with your own widget ?!
def add_required(elm):
elm['_class'] += ' required'
for t in db.tables:
for f in db[t].fields:
if db[t][f].required:
db[t][f].onrender = add_required
Creating custom widgets is exactly what I'm trying to get around. I have a bunch of fields that just need a css class added. The default SQLFORM widgets work perfectly otherwise. So instead of creating a bunch of custom widgets I can just tweak the default widgets output for ALL fields like this:There is currently no hook into the rendering of default widgets otherwise. I hope that makes sense.def add_required(elm):
elm['_class'] += ' required'
for t in db.tables:
for f in db[t].fields:
if db[t][f].required:
db[t][f].onrender = add_required
On Sunday, August 25, 2013 9:51:30 AM UTC-5, Niphlod wrote:
"Ok, the thing is that there are no hooks in rendering cause all the rendering is meant to be happen in your own widget." - I disagree. You can modify a SQLFORM after it renders. I am simply trying to achieve a similar effect at the Field level.
"Ok, the thing is that there are no hooks in rendering cause all the rendering is meant to be happen in your own widget." - I disagree. You can modify a SQLFORM after it renders. I am simply trying to achieve a similar effect at the Field level."Also, what you're trying to achieve works for input, but not for selects, list:string, etc etc. i.e." - It is working for me in inputs, selects, etc. Did you try it out?
"In any case, with your patch you just added the code you needed, it doesn't save you any typing if you import you own widgets overwriting the default ones with your own." - Recreating all of the default widgets is a lot of typing
"Again, I'm not seeing a big improvement vs the added complexity." - The patch is very simple. It just calls an onrender method if it exists after a default widget is rendered.
def add_required(elm):
elm['_required'] = ''
form = SQLFORM.factory(Field('age','list:integer', required=True,
requires=IS_IN_SET([12,23,34,45,56]),
onrender=add_required ),
Field('colors','list:string', required=True,
requires=IS_IN_SET(['red','blue','green','orange','black']),
onrender=add_required ))
"Ok, the thing is that there are no hooks in rendering cause all the rendering is meant to be happen in your own widget." - I disagree. You can modify a SQLFORM after it renders. I am simply trying to achieve a similar effect at the Field level.
I want to take advantage of bootstrap's form validation classes so I need to add a class to the field.
def dressMandatory(grid, table): """ Special representation for mandatory fields in grid """ for fieldname in table.fields: myid = '_'.join((table._tablename, fieldname, )) # icon = I(_class="icon-warning-sign") app = '' # SPAN(icon, _class="add-on") if table[fieldname].required else '' class_value = "control-group warning" if table[fieldname].required else '' grid.elements('#%s' % myid, replace=lambda el: DIV(DIV(TAG.nobr(el, app), _class="input-append"), _class=class_value)) def dressMandatoryInForm(form, *tabs): """ Special representation for mandatory fields in form """ tabs_fields = [[t[f] for f in t.fields if f != t._id.name] for t in tabs] fields_list = sum(tabs_fields[1:], tabs_fields[0]) m = form.elements()[0].elements for field in fields_list: if field.writable and field.readable and not field.compute: if field.required: m('#no_table_%s' % field.name, replace=lambda el: DIV(DIV(el, _class="input-append"), _class="control-group warning"))