Nice. I think we should add an argument to Field() that takes a dictionary of args to pass to the field's widget (or maybe just add **kwargs to Field() and pass kwargs to the widget). For now, though, if that's all you want to do, you can do something like this:
def widget(**kwargs): return lambda field, value, kwargs=kwargs: SQLFORM.widgets[field.type]. widget(field, value, **kwargs) db.define_table('mytable', Field('myfield', widget=widget(_placeholder='my placeholder', _readonly= True))) That should work for most fields -- would need some additional logic to accommodate fields that involve special widgets (e.g., fields with an IS_IN_DB validator that automatically get an options widget). Anthony On Thursday, August 8, 2013 2:11:44 PM UTC-4, Joe Barnhart wrote: > > So I'm a control-freak. I mean that in a good way. I discovered the > beauty of "placeholders" in forms and just thought "man, I really want > that!" But where to hold the placeholder data? And how do I get it to the > SQLFORM during form creation? > > At first I thought about adding YAV (yet another variable) to the Field > class. But it's getting pretty crowded in there already, and mucking with > it means changes to the base, and the devs might object to a > special-purpose hack inserted into Field just to satisfy my whims. So I > thought I'd just create my own formstyle function and use that. But wait! > The formstyle function has a defined signature now, and there is no place > to insert anything like a dictionary of placeholders indexed by field name. > Or is there? > > I created my own formstyle function, but added a keyword parameter to the > end for the placeholders dict. The signature looke like this: > > > def my_fieldstyle(form, fields, placeholders=None): > ...blah... > > > Now, the code in SQLFORM still has no idea that these placeholders exist. > But I can "wrap" this function with either a "partial function" or a > simple lambda to provide my own dictionary just before the customized > formstyle function is handed off to SQLFORM for it's main job. It looks > like this: > > > my_placeholder_dict = {'field1': 'place1', 'field2', 'place2'...} > ... > styler = lambda form, fields: my_fieldstyle(form, fields, > placeholders=my_placeholder_dict) > ... > form = SQLFORM(... formstyle=styler ...) > > > That's all there is to it! Now I can pass abundant information into the > SQLFORM creation, info that the base form creation has no idea exists. My > own custom formstyle class can interpret the extra information and add all > kinds of special features. Just a few ideas: > > 1. Provide "placehloders" for phone and date fields to show proper format > > 2. Provide dictionary of "field sets" with "legends" and create a > structured form organized the way I want > > 3. ... and many many more! > > Best of all, it only needs stupid Python tricks and doesn't muck with the > underlying structure of web2py at all! No nasty stares from the devs! > (Just kidding...) > > -- Joe > -- --- 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+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.