Overriding default field validators

18 views
Skip to first unread message

Joe Barnhart

unread,
Apr 23, 2009, 1:11:13 AM4/23/09
to web2py Web Framework
I have a special application that holds the results of swimming
matches. I store all times as floating-point "seconds" internally,
but I want to permit users to enter times as either a formatted string
(mm:ss.hh) or an integer (mmsshh).

I created my own custom validator which can take either input and
renders the result to the database as a float. It also formats the
presentation to show the times as mm:ss.hh as the preferred output
format. So far, so good...

Now the problem. The underlying field is a 'double' which resolves to
a float on Sqllite. The built-in field validator for FLOAT is still
active, so it prevents anyone from entering ":" characters. Is there
no easy way to completely replace the default field validator? This
will limit my use of the custom validation feature, I fear.

Álvaro Justen [Turicas]

unread,
Apr 23, 2009, 1:49:03 AM4/23/09
to web...@googlegroups.com

Default validators are in definition of function sqlhtml_validators,
in gluon/sql.py but I think it is not "normal" to a validator don't be
rewrited. Can you show your models and new validator code?

--
Álvaro Justen
Peta5 - Telecomunicações e Software Livre
21 3021-6001 / 9898-0141
http://www.peta5.com.br/

Joe Barnhart

unread,
Apr 23, 2009, 4:19:04 AM4/23/09
to web2py Web Framework
I found out a bit more regarding the problem. It seems that there is
an ajax field validator running which requires the input be suitable
for a "double" variable. The SQLFORM sets the "class" attribute of
the field to "double" because that is the underlying data type.

Even though my special validator is able to take strings and render
them into a double, it never gets the chance because the ajax layer is
rejecting all characters other than [0-9] and [.]. I need to override
the "class" attribute on this particular field when rendered by
SQLFORM.

In the meantime, I am using a manually-created FORM to get by.

-- Joe B.


On Apr 22, 10:49 pm, Álvaro Justen [Turicas] <alvarojus...@gmail.com>
wrote:

mdipierro

unread,
Apr 23, 2009, 2:26:33 PM4/23/09
to web2py Web Framework
look into jquery $(...).class(...)

DenesL

unread,
Apr 23, 2009, 4:08:17 PM4/23/09
to web2py Web Framework
On Apr 23, 4:19 am, Joe Barnhart <joe.barnh...@gmail.com> wrote:
> I found out a bit more regarding the problem. It seems that there is
> an ajax field validator running which requires the input be suitable
> for a "double" variable. The SQLFORM sets the "class" attribute of
> the field to "double" because that is the underlying data type.
>
> Even though my special validator is able to take strings and render
> them into a double, it never gets the chance because the ajax layer is
> rejecting all characters other than [0-9] and [.]. I need to override
> the "class" attribute on this particular field when rendered by
> SQLFORM.

It is not an ajax "validator", it is jQuery code introduced by
web2py_ajax.html using $('input.double').attr('onkeyup', ... ).

Which is why you need to change the class, as you said, to prevent
jQuery from modifying your field. The easiest way is using a widget,
before calling SQLFORM:

db.your_table.your_double.widget= lambda field, value:
INPUT(_type='text',
_id='%s_%s' %(field._tablename, field.name),
_class='mydouble',
_name=field.name,
value='' if not value else str(value),
requires=field.requires)
Reply all
Reply to author
Forward
0 new messages