I am using SQLFORM.factory to create a custom form. In this form I have a field as:
Field('useraccount','unicode',default=None)
So as per my understanding, when user does not supply this value, request.vars.useraccount will be None. But it rather shows empty string value.
I can convert it to None by checking for empty string value but I do not want to do that.
Please suggest me any solution.
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
When the form is submitted, if the "useraccount" input field is empty, the browser still sends an empty string for that field, so that is what goes into
request.vars.useraccount
. Since an empty string is a valid value for a string field, web2py does not override that and convert it toNone
-- if you want it to be converted, you must do so explicitly. One option is to create a custom validator that converts empty strings to None.A validator must be a callable that takes a value and returns a tuple including a value (the original value or possibly a transformed version of it, as in this case) and either
None
(if there is no error) or an error message. The above validator is simple enough that it could easily be moved to a lambda:Also, note that setting the
default
value inSQLFORM.factory
is only useful for pre-populating the form fields (and in this case, you are not pre-populating with anything). The only other use of thedefault
attribute is for database inserts, but since thisField
object is not part of a DAL table model, it won't be used for inserts anyway.