custom maxlengths on create/edit grid

32 views
Skip to first unread message

JSalvat

unread,
Jul 4, 2018, 6:31:50 AM7/4/18
to web2py-users
Hi,

How to custom an input field width   class='col-sm-x'  in create/edit forms that come from SQLFORM.grid

On SQLFORM can be done this way:

form = SQLFORM(db.movimiento)
if form.element('#movimiento_cantidad'):
     wclass = form.element('#movimiento_cantidad')['_class']
     form.element('#movimiento_cantidad')['_class'] = wclass + ' col-sm-3'
     form.element('#movimiento_cantidad')['_maxlength'] = 8

but when the form is generic from SQLFORM.grid  how can it be done?

Thanks for any help if it could be done.

黄祥

unread,
Jul 4, 2018, 6:58:12 AM7/4/18
to web2py-users
think there is maxtextlength attribute on sqlform.grid signature, but not sure if it suitable with your css class definition
ref:

best regards,
stifan

JSalvat

unread,
Jul 4, 2018, 7:13:04 AM7/4/18
to web2py-users
Yes, but the  maxtextlength attribute works only on the grid, and I think is not accesible from the generic create/edit form created by the grid. How the maxtextlength attribute be used in the form?

Anthony

unread,
Jul 4, 2018, 10:16:54 AM7/4/18
to web2py-users
First, you can use the same approach with the grid that you are using with SQLFORM, as the grid simply produces a SQLFORM when creating or updating records. Note, web2py DOM objects have an .add_class() method, so you can use that instead of manually extracting and updating the "_class" attribute.

Another option is to specify a custom widget for the field in question, either when the db table is defined or later:

Field('cantidad', widget=lambda f, v: SQLFORM.widgets.string.widget(f, v, _class='col-sm-3', _maxlength=8))

Anthony
Message has been deleted

JSalvat

unread,
Jul 4, 2018, 12:43:04 PM7/4/18
to web2py-users
Thanks Anthony, this really does the perfect job, much simpler !!
Reply all
Reply to author
Forward
0 new messages