Note, you might as well also add 'type': 'string' to your dictionary, and 
maybe 'length': 20. You can also give yourself some flexibility by creating 
a function:

def phone_field(name, **kwargs):
    defaults = {'type': 'string',
                'length': 20,
                'requires': IS_EMPTY_OR(IS_PHONE()),
                'widget': lambda fld,val: SQLFORM.widgets.string.widget(
                    fld, val, _type='tel', _class='form-control')}
    defaults.update(**kwargs)
    return Field(name, **defaults)

Anthony

On Thursday, March 23, 2017 at 3:54:58 PM UTC-4, Joe Barnhart wrote:
>
> Here is a bit of syntactic sugar I use for creating fields with less 
> typing.  And better consistency, of course -- there always has to be a good 
> reason for my lazy keyboard-saving shortcuts!
>
> I have a number of fields which are for specific data types, such as phone 
> numbers.  I gather the "common" parts of the Field definition into a 
> dictionary:
>
> phone_fld = {'requires':IS_EMPTY_OR(IS_PHONE()),
>              'widget':lambda fld,val: SQLFORM.widgets.string.widget(fld,
> val,_type='tel',_class='form-control')}
>
> When defining the field in a table, I invoke the dictionary at the end of 
> the Field definition:
>
> Field("homephone","string",length=20,label=T("Home phone"),**phone_fld),
> Field("workphone","string",length=20,label=T("Work phone"),**phone_fld),
> Field("cellphone","string",length=20,label=T("Cell phone"),**phone_fld),
>
> Now the field is created exactly as I want.  I keep a list of these 
> "helpers" in a module which I can call out when creating tables.  It really 
> saves the typing and allow me to control how my fields are rendered on one 
> place instead of scattered among all the tables.
>
> -- Joe
>
>

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
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 [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to