Not sure if somebody noticed before, but the show_if syntax (backed by
web2py.js) is actually breaking if you use SQLFORM.factory to generate your
form.
Example:
db.define_table('person', Field('name', 'string'), Field('show_trigger',
> 'boolean'))
> db.person.name.show_if = (db.person.show_trigger == True)
When show_if is processed, special attributes are added to inputs.
data-show-trigger is among them, and is used in the javascript code to
determine the id of the element that triggers display.
In our previous example, the data-show-trigger generated by our model is
person_show, and web2py.js will try to match this id.
*In a factory form, the tablename of a field is either specified or
hard-coded ('no_table').*
Example:
> form = SQLFORM.factory(db.person)
The generated input for the field "person.name" willl have the following id
: no_table_name.
*The mismatch between SQLFORM.factory's input and and data-show-trigger
produces a javascript error, which of course blocks any further javascript
code from being executed.*
*Here is the modification I suggest in order to fix this :*
In *gluon.sqlhtml.py*, replace the last line :
return SQLFORM(DAL(None).define_table(table_name, *fields), **attributes)
With the following code:
# Generate SQLFORM
form = SQLFORM(DAL(None).define_table(table_name, *fields), **attributes)
# Replace data-show-trigger (cf. 'show_if') since we changed the table
name
def replace_triggers(fields):
for field in fields:
if isinstance(field, Field) and getattr(field, 'show_if', None):
form.element('input', _id='%s_%s' % (table_name, field.name),
replace=lambda t: t.update(**{
'_data-show-trigger':'%s_%s' % (table_name, field.show_if.first.name)}))
elif isinstance(field, Table):
table = field
# recursive call
replace_trigger([field for field in table])
replace_triggers(fields)
return form
This way, the hard-coded tablename in SQLFORM.factory is also applied to
any show_if in the form.
--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.