Thank you both, Massimo and Anthony. This is exactly the kind of pointer I was looking for.
Ed On Oct 8, 3:37 pm, Anthony <abasta...@gmail.com> wrote: > I think he wants to check to make sure the cleaned number isn't already in > the database, so if defining a custom validator for the cleaning, wouldn't > both validators be needed? > > FORM(INPUT(_name='phone', requires=[MyValidator(), IS_NOT_IN_DB(db, > 'tablename.phone')])) > > In this case, MyValidator returns the cleaned version of the number, and > that value is then passed to the IS_NOT_IN_DB validator. > > The lambda version should work too, though, no? I suppose another option > would be: > > FORM(INPUT(_name='phone', requires=[lambda v: (cleanUpNumber(v), None), > IS_NOT_IN_DB(db, 'tablename.phone')])) > > Of course, if the cleaning validator needs to do any error checking and > possibly return an error, then it would be better to write a full custom > validator. > > Note, in all these cases, the validator(s) end up transforming the submitted > number to the cleaned version, so form.vars.phone will end up cleaned, so > you won't need to do a separate cleaning step. > > Anthony > > > > > > > > On Saturday, October 8, 2011 11:17:16 AM UTC-4, Massimo Di Pierro wrote: > > > More like this: > > > class MyValidator(object): > > def __call__(self,value): > > return (cleanUpNumber(value), None) > > > FORM(INPUT(_name='phone',requires=MyValidator()) > > > On Oct 8, 7:48 am, Anthony <abas...@gmail.com> wrote: > > > Something like: > > > > FORM(INPUT(_name='phone', > > > requires=lambda v: IS_NOT_IN_DB(db, > > > 'yourtable.phone')(cleanUpNumber(v)))) > > > > Anthony > > > > On Saturday, October 8, 2011 8:10:51 AM UTC-4, Ed Greenberg wrote: > > > > > I have a table of US phone numbers in ten digit form. When I store > > > > them, I take out all non-numeric characters using: > > > > > def cleanUpNumber(number): > > > > return re.sub(r'\D',"",number)[-10:] > > > > > I use a FORM() to get the numbers. Not a SQLFORM or CRUD. > > > > > I would like to use IS_NOT_IN_DATABASE(...) to make sure that I don't > > > > get duplicates. > > > > > How can I get the validator to run the values through cleanUpNumber() > > > > before looking in the database? > > > > > Ed