Question

As the title says, how do I trim leading and trailing whitespace from input fields in web2py? I've tried the CLEANUP() validator which works but unfortunately it strips out other special characters which I do not want stripped out (for example, ¥).

Also what are the characters it's stripping out? This is not clear to me.

Était-ce utile?

La solution

CLEANUP takes a regex argument, which defaults to '[^\x09\x0a\x0d\x20-\x7e]'. It first strips leading and trailing whitespace and then removes any characters that match the regex. Since you only need to remove the whitespace, you can supply an empty regex:

Field('myfield', requires=CLEANUP(''))

You could also achieve the same with a custom validator:

Field('myfield', requires=lambda v: (str(v).strip(), None))
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top