How to put a placeholder in a grid search_form to label the search field

26 views
Skip to first unread message

David Manns

unread,
Sep 2, 2025, 12:44:56 PM (6 days ago) Sep 2
to py4web
This used to work:

search_form=Form([
Field('account', 'reference CoA', default=request.query.get('account'),
requires=IS_EMPTY_OR(IS_IN_DB(db, 'CoA', '%(Name)s', zero="account?"))),
...
Field('notes', 'string',
widget=lambda field, value: INPUT(_name='notes', _type='text',
_value=request.query.get('notes'), _placeholder="notes?")),
Field('reference', 'string',
widget=lambda field, value: INPUT(_name='reference',
_type='text', _value=request.query.get('reference'), _placeholder="ref?"))
],
keep_values=True, formstyle=FormStyleBulma)



For the account Field, the IS_IN_DB zero parameter provides a nice way to identify the box in the form. This still works.

For the text search boxes, notes and reference, the only way to identify the form box I could find was to use a custom widget with a placeholder. This no longer works, I get an error "Error Ticket: The usage of custom widgets has changed. If you used to override the widget for a field, don't instantiate the class anymore: old: FormStyle.widgets["fieldname"] = RadioWidget() new: FormStyle.widgets["fieldname"] = RadioWidget"

Massimo DiPierro

unread,
Sep 3, 2025, 12:59:24 AM (5 days ago) Sep 3
to py4web
We moved away from the idea of putting presentation information into the database model. It does not belong there and it can be confusing. 
You can do it with a custom Widget or you can do:

form = Form(...)
form.structure.find("#{tablename}_{fieldname}")[0]["_placeholder"] = "write something"

In your case since there is no table and the field is called "notes"

form.structure.find("#no_table_notes")[0]["_placeholder"] = "notes?"

It is a bit of a hack but works.

laundmo

unread,
Sep 3, 2025, 6:07:17 AM (5 days ago) Sep 3
to py4web
>  We moved away from the idea of putting presentation information into the database model 

Could've fooled me:

Field.label
Field.represent
Field.requires
Field.required
Field.writable
Field.readable
Field.default

All those are (sometimes exclusively, sometimes partially) storing some king of presentation information

David Manns

unread,
Sep 3, 2025, 10:08:13 AM (5 days ago) Sep 3
to py4web
Thanks, the hack works!

The documentation of custom fields I find totally incomprehensible. I suspect it was always inaccurate because it doesn't show the subclassing in it's example, and it also needs to be changed to reflect the new changes.
Reply all
Reply to author
Forward
0 new messages