문제

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.

도움이 되었습니까?

해결책

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))
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top