How do I apply placeholder text to web2py forms?

1,818 views
Skip to first unread message

Carl

unread,
Jun 23, 2011, 10:24:44 AM6/23/11
to web2py-users
I'm using custom forms in my views using this format:
{{=form.custom.widget.first_name}}

I'd like to use HTML's placeholder attribute to input tags: e.g.,
<input type="text" placeholder="first name" />

Today: what are the ways to enable this? Obviously happy to drop the
{{=form.custom.widget.first_name}} format and use something else.

Tomorrow: might it be a good idea to add placeholder attribute to
db.Field() in a similar fashion to how 'label' has already been added?

Massimo Di Pierro

unread,
Jun 23, 2011, 10:55:19 AM6/23/11
to web2py-users
form.element(_id='...')['_placeholder']='...'

Anthony

unread,
Jun 23, 2011, 12:04:55 PM6/23/11
to web...@googlegroups.com
A couple other methods:
 
1. In the controller or view:
 
form.custom.widget.first_name.update(_placeholder="first name")
 
or
 
2. Customize the db.yourtable.first_name widget:
 
db.define_table('yourtable', Field('first_name',
    widget=lambda field,value: SQLFORM.widgets.string.widget(field, value, _placeholder='first name')),
    [rest of table definition])
 
or specify it after table definition via db.yourtable.first_name.widget=lambda...
 
Method #2 will apply the change to all forms that include that field.
 
There should probably be an easier/more straightforward way to do this, though. All the widgets take keyword arguments, but it doesn't look like there's an easy way to pass them in when the widgets are associated with db table fields.
 
Anthony
 

On Thursday, June 23, 2011 10:55:19 AM UTC-4, Massimo Di Pierro wrote:
form.element(_id='...')['_placeholder']='...'

Carl Roach

unread,
Jun 23, 2011, 12:19:06 PM6/23/11
to web...@googlegroups.com
thanks.

feels right to have this data in the view but I can see the advantage
of 'declare once' in the model.
I agree Anthony, a solution could be baked into web2py as 'label'
already has been. html5 is here and adoption is pretty good thanks in
large part to webkit.

Message has been deleted

Anthony

unread,
Jun 23, 2011, 12:29:10 PM6/23/11
to web...@googlegroups.com
On Thursday, June 23, 2011 12:19:06 PM UTC-4, Carl wrote:

I agree Anthony, a solution could be baked into web2py as 'label'
already has been. html5 is here and adoption is pretty good thanks in
large part to webkit.

Rather than a specific argument just for 'placeholder', it should be possible to specify any arbitrary <input> tag attribute (there are lots of them) -- the widgets accept such attributes via a **attributes argument, but doesn't look like there's an easy way to pass them in for widgets associated with db table fields.
 
Anthony

Carl Roach

unread,
Jun 23, 2011, 12:46:38 PM6/23/11
to web...@googlegroups.com
gotcha. something more open-ended.

davedigerati

unread,
Jul 28, 2013, 1:41:22 PM7/28/13
to web...@googlegroups.com
Been banging my head against the wall with this, trying to move from a simple form to a sqlform, and placeholder is not working:
in my view this works fine:
{{=form.custom.widget.tm_home}}
but
{{=form.custom.widget.tm_home["_placeholder"] = "Home Team Name"}}
or
{{=form.custom.widget.tm_home['_placeholder] = 'Home Team Name'}}
gives
<type 'exceptions.SyntaxError'> keyword can't be an expression (newGame.html, line 99)
and
{{=form.custom.widget.tm_home(_placeholder = "Home Team Name")}}
gives
<type 'exceptions.TypeError'> 'INPUT' object is not callable

I'm assuming this is a formatting problem in my code (hence the variations above I scraped from google and this thread) but what exactly?
Thanks!

(And +1 for more input attributes)

Anthony

unread,
Jul 28, 2013, 1:56:24 PM7/28/13
to web...@googlegroups.com
Should be:

{{form.custom.widget.tm_home["_placeholder"] = "Home Team Name"}}
{{=form.custom.widget.tm_home}}

You can move that first line into the controller if you like.

Anthony

davedigerati

unread,
Jul 28, 2013, 2:07:18 PM7/28/13
to web...@googlegroups.com
Thank you Anthony- U nailed it.

But can I say GRRRRR!?
I had tried that but copy/pasted the lines so the placeholder still had an = to output and I'm guessing that was what it barfed on....

Is there a good, short/sweet reference for format of single vs double quotes, parentheses vs brackets?  I notice differences in the answers above and would like to understand convention better.
Again, thank you!

Anthony

unread,
Jul 28, 2013, 9:05:56 PM7/28/13
to web...@googlegroups.com
No difference between single and double quotes, though you typically use single quotes unless you are quoting something that contains single quotes, in which case you may use double quotes (or triple single/double quotes -- ''' or """).

Parentheses are used when calling functions or methods, and brackets are used for specifying keys of dictionaries or slicing lists.

All of this is just standard Python, so any Python reference should cover this.

Anthony

Richard Vézina

unread,
Jul 29, 2013, 3:20:23 PM7/29/13
to web2py-users
I already suggest this... But we could make a Field() methode that let insert HTML attribute to a field like html5 attributes or any other attribute without the obligation to create a custom widget or manipulate a generated form, etc.

Ex.: 

Field('input', 'type', ..., html_attr=dict('_placeholder'='my place holder text...'),

The html_attr should be smart and not set wrong attr to a given html tag if this property is not a member of the tag attributes...

Not sure how it could be done, but it could be really usefull to help improve form with new html5 attributes...

Thanks

Richard


--
 
---
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 web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Giacomo Dorigo

unread,
Aug 21, 2014, 10:09:07 AM8/21/14
to web...@googlegroups.com
Method 1 can be easily generalized in a function that set the placeholder for all sqlform fields and strip away all labels and comments:

def autoset_form_placeholders(form, form_labels=False, form_comments=False):
    for f in form.fields:
        if f == 'id':
            pass
        else:
            form.custom.widget[f].update(_placeholder=f)
    if not form_labels:
        form.elements('.w2p_fl', replace=None)
    if not form_comments:
        form.elements('.w2p_fc', replace=None)

Joe Barnhart

unread,
Aug 25, 2014, 1:46:17 AM8/25/14
to web...@googlegroups.com
If you're using SQLFORM, I did come up with a cute little trick that uses a lambda to add placeholders to a custom formstyle definition.


-- Joe
Reply all
Reply to author
Forward
0 new messages