how do I add form attributes to SQLFORM.factory?

98 views
Skip to first unread message

Oliver Holloway

unread,
Mar 9, 2015, 5:53:24 PM3/9/15
to web...@googlegroups.com

How do I get the form to autofocus the cursor into the text box? Here's one of the approaches I've tried. 

  attributes = {'_autofocus':'autofocus', '_type':'tel', '_autocomplete':'off', '_style':'height:80px; width:80px'}
  score_box = SQLFORM.factory(Field('score', 'integer'), **attributes)

Interestingly, the autocomplete attribute is definitely getting through, I've tested that repeatedly. None of the other ones are. 

I did see the post about doing this with a form.custom.widget setting, but couldn't figure out how to make that work in my case. I've also experimented with using keepvalues=True, from looking through the book.

Any help is much appreciated. This is the last thing on my list to have this demo ready. 

Oliver Holloway

unread,
Mar 10, 2015, 2:41:08 PM3/10/15
to web...@googlegroups.com
Read part of the book, tried with just FORM. The following worked.

  score_box = FORM(INPUT(_name='score',requires=IS_NOT_EMPTY(),
                         _style='height:80px; width:80px; font-size:50px;',
                         _autofocus='autofocus', _type='tel', _autocomplete='off'
                         )
                   )


Anthony

unread,
Mar 10, 2015, 3:44:18 PM3/10/15
to web...@googlegroups.com
If the attributes are supposed to apply to the HTML <form> element, then your approach is correct. But if the attributes belong to individual <input> and <textarea> elements, then you must use the "widget" argument of the Field() constructor or update the widget after creation. For example:

Field('score', 'integer',
      widget
=lambda f, v: SQLFORM.widgets.integer.widget(f, v, _autofocus=True))

or after defining the form:

form.custom.widget.score['_autofocus'] = True

Anthony
Reply all
Reply to author
Forward
0 new messages