Repository: web2py/web2py

Issue:
IS_NOT_EMPTY and IS_EMPTY_OR should not strip whitespace
When checking values for being empty, the IS_NOT_EMPTY and IS_EMTPY_OR validators first strip whitespace to ensure the value isn't just whitespace. The problem is that these validators then return the stripped version of the value instead of the original value. This means that values that are not just whitespace but include some whitespace will be transformed before being inserted in the database. For example, " hello " will be transformed to "hello". This behavior is unexpected, as the purpose of these validators is not to remove whitespace from otherwise non-empty strings.

It may be appropriate to strip whitespace in order to determine whether a string should be considered empty (and we probably have to continue that to maintain backward compatibility), but I think returned the stripped value instead of the original value should be considered a bug. If the developer wants to strip whitespace from submitted values, that should be handled explicitly via another validator or some other method.

See https://groups.google.com/forum/#!topic/web2py/_SfUxePTaN0.


Discussion:
I think that is just midify this peace:
https://github.com/web2py/web2py/blob/master/gluon/validators.py#L991-L999

```python
def is_empty(value, empty_regex=None):
    """test empty field"""
    if isinstance(value, (str, unicodeT)):
        value = value.strip()
        if empty_regex is not None and empty_regex.match(value):
            value = ''
    if value is None or value == '' or value == []:
        return (value, True)
    return (value, False)
```

Using the ```.strip()``` just in the argument the function ```isinstance``` and leave the original value in rest of function.

**Steps to do this**

- [ ] Leave the ```.strip()```just the isinstance method.
- [ ] Update the docstrings to sync the behavior updated

Resolving patch:
diff --git a/gluon/validators.py b/gluon/validators.py
--- a/gluon/validators.py
+++ b/gluon/validators.py
@@ -989,14 +989,15 @@ def formatter(self, value):
 
 
 def is_empty(value, empty_regex=None):
+    _value = value
     """test empty field"""
     if isinstance(value, (str, unicodeT)):
         value = value.strip()
         if empty_regex is not None and empty_regex.match(value):
             value = ''
     if value is None or value == '' or value == []:
-        return (value, True)
-    return (value, False)
+        return (_value, True)
+    return (_value, False)
 
 
 class IS_NOT_EMPTY(Validator):


Test patch:
diff --git a/gluon/tests/test_validators.py b/gluon/tests/test_validators.py
--- a/gluon/tests/test_validators.py
+++ b/gluon/tests/test_validators.py
@@ -434,21 +434,21 @@ def test_IS_NOT_EMPTY(self):
         rtn = IS_NOT_EMPTY()('x')
         self.assertEqual(rtn, ('x', None))
         rtn = IS_NOT_EMPTY()(' x ')
-        self.assertEqual(rtn, ('x', None))
+        self.assertEqual(rtn, (' x ', None))
         rtn = IS_NOT_EMPTY()(None)
         self.assertEqual(rtn, (None, 'Enter a value'))
         rtn = IS_NOT_EMPTY()('')
         self.assertEqual(rtn, ('', 'Enter a value'))
         rtn = IS_NOT_EMPTY()('  ')
-        self.assertEqual(rtn, ('', 'Enter a value'))
+        self.assertEqual(rtn, ('  ', 'Enter a value'))
         rtn = IS_NOT_EMPTY()(' \n\t')
-        self.assertEqual(rtn, ('', 'Enter a value'))
+        self.assertEqual(rtn, (' \n\t', 'Enter a value'))
         rtn = IS_NOT_EMPTY()([])
         self.assertEqual(rtn, ([], 'Enter a value'))
         rtn = IS_NOT_EMPTY(empty_regex='def')('def')
-        self.assertEqual(rtn, ('', 'Enter a value'))
+        self.assertEqual(rtn, ('def', 'Enter a value'))
         rtn = IS_NOT_EMPTY(empty_regex='de[fg]')('deg')
-        self.assertEqual(rtn, ('', 'Enter a value'))
+        self.assertEqual(rtn, ('deg', 'Enter a value'))
         rtn = IS_NOT_EMPTY(empty_regex='def')('abc')
         self.assertEqual(rtn, ('abc', None))
 
@@ -780,7 +780,7 @@ def test_IS_EMPTY_OR(self):
         rtn = IS_EMPTY_OR(IS_EMAIL())('abc')
         self.assertEqual(rtn, ('abc', 'Enter a valid email address'))
         rtn = IS_EMPTY_OR(IS_EMAIL())(' abc ')
-        self.assertEqual(rtn, ('abc', 'Enter a valid email address'))
+        self.assertEqual(rtn, (' abc ', 'Enter a valid email address'))
         rtn = IS_EMPTY_OR(IS_IN_SET([('id1', 'first label'), ('id2', 'second label')], zero='zero')).options(zero=False)
         self.assertEqual(rtn, [('', ''), ('id1', 'first label'), ('id2', 'second label')])
         rtn = IS_EMPTY_OR(IS_IN_SET([('id1', 'first label'), ('id2', 'second label')], zero='zero')).options()