Hello all.
I am attempting to make a custom field of sorts that is composed of other
field types. For example this one field will write html for x number of
fields and will be stored in the db as list of x elements.
If there is some better way in web2py to do this please let me know!
So far this is what I have done. First I created a WidgetFactory:
import gluon.sqlhtml as sqlhtml
import gluon.dal as dal
def MultiWidgetFactory(*fields):
class MultiWidget(sqlhtml.FormWidget):
_class = "multi"
@classmethod
def widget(cls, field, value, **attributes):
_id = '%s_%s' % (field._tablename, field.name)
_name = field.name
items = []
for f in fields:
#required!
f._tablename = _id
items.append(SQLFORM.widgets[f.type].widget(f,'text',
requires=dal.sqlhtml_validators(f)))
return CAT(*items)
return MultiWidget
(Yes, setting 'text' is just for debugging purposes.)
next I create a field in a table like so:
Field('schools','text', widget=MultiWidgetFactory(Field('school_name'),Field
('major_minor_fields'),Field('year_graduated')).widget)
Later I call and output form:
form = SQLFORM(db.table, fields=['schools'])
This works great. The form outputs each of the fields in my multiwidget,
and each of them are validated.
Now for the issue: for the life of me I can't figure out how to persuade
form.process/accepts/validate to take my form!
I always get an error: "<Storage {'schools': 'no data'}>" despite the fact
that I directly edit request.vars and add a schools key before calling
process.
I looked through the code of process, accept, and validate in the FORM and
SQLFORM objects, but everything I am reading tells me it should just work.
If someone could please help, or suggest an easier alternative, I would
appreciate it.
Kory Prince