This is a three step process:

1) disable JS validation as float for this field

form.element('input[name=height]')['_class']='feet'

2) create a custom validator that filters data in and formats data out

class FEET:
     def __init__(self,error_message='invalid'):
self.error_message=error_message
     def __call__(self,value):
           # converts "3ft 4.5in" input into 1.0287 meters for
DB
           import re
           match=re.compile("(?P<feet>\d+)ft\s+(?P<inches>\d+(\.\d
+)?)in").match(value)
           if not match: return (value, self.error_message)
           return 0.30480*(int(match.group('feet'))
+float(match.group('inches'))/12) #meters of course
      def formatter(self,value):
           # converts 1.0287 meters from DB into "3ft 4.5in" output
           feet = value/0.30480
           feet, inches = int(feet), (feet-int(feet))*12
           return '%ift %fin' % (feet, inches)

db.define_table('foo',
   ...
   Field('height', 'double', requires=FEET()),
   ...
)

3) (optional but nice touch) create a JS validator for CSS class
'feet'

jQuery('input.double').keyup(function()
{this.value=do_something(this.value);});

you have to write the function do_something to parse ft and in and
output the proper formatting.

Massimo

On Jun 4, 11:09 am, Keith <[email protected]> wrote:
> I'm new to web2py and the I did this previously was with javascript
> but I was hoping to avoid that this go around.
>
> In my database table I have a single field something like this below:
>
> db.define_table('foo',
>    Field('height', 'double'),
>
> In the controller/view I need to be able to translate that so when
> viewing a record they see:
> Feet, Inches, and Fractions of a Inch.
>
> When adding a record it would need to be a text field for feet and
> drop down lists for inches and fractions of a inch.
>
> I know how to do that in javascript, but I don't like relying on
> javascript if possible.  I'm hoping there is a way to do this in the
> controller or model.
>
> Thanks for any suggestions.

Reply via email to