Maybe you could monkey patch SQLFORM:
SQLFORM.widgets.double = my_double_widget
Technically, that won't change the "widget" attribute of the DAL Field, but
it will cause SQLFORM to use your widget instead of the default
DoubleWidget whenever it encounters a double field that doesn't have an
explicit widget defined.
Note, in that case, you'll have to define your widget as a class with a
static method called "widget" -- something like (not tested):
class MyDoubleWidget(object):
@staticmethod
def widget(field, value):
if "e" in value:
if value.endswith(".00"):
value = value[0:len(value)-3]
return INPUT(_name=field.name,
_id="%s_%s" % (field._tablename, field.name),
_class=field.type,
_value=value,
requires=field.requires)
Anthony
On Wednesday, March 7, 2012 4:01:38 AM UTC-5, Kimmo wrote:
>
>
> I made a (ugly hack) custom widget for all the Fields that have double
> type. Now E notation and normal double values work perfectly (edit /
> save / viewing).
> How do i set this widget as a default widget for all the Fields that
> have double type in my model? I would rather like to avoid setting
> this to all double Fields manually:
> Field('value', 'double', notnull=True, widget=my_double_widget)
>
> def my_double_widget(field, value):
> if "e" in value:
> if value.endswith(".00"):
> value = value[0:len(value)-3]
> #print value
> return INPUT(_name=field.name,
> _id="%s_%s" % (field._tablename,
> field.name),
> _class=field.type,
> _value=value,
> requires=field.requires)
>
>
> - Kimmo
>
> On Mar 5, 8:30 am, Anthony <[email protected]> wrote:
> > On Monday, March 5, 2012 7:22:05 AM UTC-5, Massimo Di Pierro wrote:
> >
> > > Actually I think the problem is simply the JS validator. Try comment
> this
> > > line in static/js/web2py.js
> >
> > > doc.on('keyup', 'input.double, input.decimal',
> > >
> function(){this.value=this.value.reverse().replace(/[^0-9\-\.,]|[\-](?=.)|[
> \.,](?=[0-9]*[\.,])/g,'').reverse();});
> >
> > Note, the above Javascript only has an effect if the class of the input
> > element is "double". By default, SQLFORM adds a "double" class to input
> > elements for fields of type double, but you can override that by
> explicitly
> > specifying an alternate widget for the field (you could create a custom
> > widget, but I think a standard string widget should work in this case).
> So,
> > in your table definition:
> >
> > db.define_table('mytable', Field('myfield', 'double',
> > widget=SQLFORM.widgets.string.widget))
> >
> > That will give the input field a class of "string" instead of "double",
> so
> > the above Javascript will no longer affect it.
> >
> > Anthony