On Oct11, 4:21am, Thadeus Burgess <[email protected]> wrote: > > > On Oct 10, 7:52 am, devnull <[email protected]> wrote: > > > > Is there an option or a validator that will strip whitespace before > > > > applying remaining validators for a given field on a form? > > > > > Somewhat related: I tried the CLEANUP validator but the resulting > > > > variable still had characters like !...@#%^ in it. Maybe I > > > > mis-understood > > > > its purpose... perhaps it strips special characters for the other > > > > validators, but then when everything is done the original typed value > > > > gets sent? It would be cool if there were a validator that took a > > > > regex or a list of characters and stripped those from the input (so a > > > > phone field might ignore everything except digits).
> > On Oct 10, 10:02 am, mdipierro <[email protected]> wrote: > > I agree. Sometimes I think stripping should be a default for non-text, > > non-blob fields. Pros/cons? > > Breaks backwards compatability. So under our banner of web2pyism, we can't > do it. > > However, devnull, try this, this will alter the input before you validate it > through the form, so you can apply operators, such as stripping, or > .capitalize() or .upper() or anything really :P > > def decode(string, strip="!...@#$%^&*()"): > newstr = "" > for char in string: > if char not in strip: > newstr += char > return newstr > > def myaction(): > if request.vars: > request.vars.fieldname = decode(request.vars.fieldname) > form = SQLFORM(db.tablename) > > if form.accepts(request.vars, session): > response.flash = "yay" > elif form.errors: > response.flash = "nay" > else: > response.flash = "hey" > > -Thadeus > How about adding a new parameter for some old validators? That will not break backward compatibility. Here is how. class CLEANUP(Validator): def __init__(self,dust=None): # dust can be None, or a list of chars (aka string), or a regex. # If it is a string, it specifis the set of characters to be removed. # If omitted or None, defaults to removing whitespace. # If it is a regex, it removes chars met by the regex. self.dust=dust def __call__(self,value): # the implementation And we can add the new parameter dust for IS_IN_DB too. class IS_IN_DB(Validator): def __init__(self, ......, dust=None): # mentioned above --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "web2py-users" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/web2py?hl=en -~----------~----~----~----~------~----~------~--~---

