Repository: web2py/web2py

Issue:
SQLFORM.factory Returns Date field as string (str) instead of date (in form.vars) as in 2.14.06
After upgrade to 2.17.1 (from 2.14.06)
SQLFORM.factory  date filed is returned as 'str' type  field instead of 'date'

As a bonus issue, the session.flash does not work either (see below).
 
**To Reproduce**
Run the following function and check the form.vars.today value printed

function:
```

def form_test():
    form = SQLFORM.factory(
        Field('today', 'date', label='today', writable=True, default=request.now.date()),
        buttons=[   TAG.button(T('Roll back'), _type="submit", _class="btn btn-primary")
                ,   TAG.button(T('Cancel'), _type="button",_onClick="parent.location='%s' ;return false" % URL('default', 'form_test'))
                 ], table_name='test_tbl',
    )
    title=T("Form test")

    if form.process(session=None, formname='test').accepted:
        print form.vars.today,type(form.vars.today) #this line prints 'str' as the field type 
        session.flash = T('Message that not flashing') #this message does not flash
        redirect(URL('default', 'form_test'))
    elif form.errors:
        response.flash = 'form has errors'
    else:
        response.flash = T('Select items to refund')
    return locals()

```
The view (form_test.html):

```
{{left_sidebar_enabled=right_sidebar_enabled=True}}
{{extend 'layout.html'}}
{{block header}}
 {{end}}
{{block left_sidebar}}
 {{end}}
 {{block right_sidebar}}
 {{end}}
 <div class="alert alert-success" role="alert"><h3 align=center>
         {{=title}}
              </h3>
 </div>
    <div class="well">
	<div class="row">
        <div class="col-md-9">
        <h4>{{=T('Fields with writable=False shows None')}}</h4>
        {{=form.custom.begin}}
        <table id="mytable" class="table table-bordred table-striped">
                   <thead>
                   </thead>
        
    <tbody>
      <tr><td>{{=form.custom.widget.tag_product_name}}</td>
    </tr>
   <tr>
   <td align="right">{{=form.custom.widget.today}}</td>
    </tbody>
    </table> 
            {{=form.custom.submit}}
            {{=form.custom.end}}
</div>
</div>
</div>

```


**Expected behavior**
Return field type as  'date'  (as in web2py version 2.14.06)



Discussion:
That's weird. If you add a requires=IS_DATE() to the field does it fixes the problem?
Yes That's helped. I got <type 'datetime.date'>
This was not required in 2.14.06. 
In my opinion it should not be necessary. If a field is defined as date , it should return a date
Am I missing something ?
> Am I missing something ?

@icodk no you're not, it's a web2py or pydal bug, it should be applying the default validators since you didn't provide one. The default validator for a "date" field is IS_DATE. I suspected this was the problem which is why I asked you to try this workaround.  
  
Now we just have to find out why aren't the default validators web2py defines being applied in this instance to find where the bug is.
> Now we just have to find out why aren't the default validators web2py defines being applied in this instance to find where the bug is.

The default `validators_method` is set to `None` when `DAL` is created with a falsy `uri` (See. https://github.com/web2py/pydal/blob/master/pydal/base.py#L483). When this line is removed the default `validators_method` are applied. @gi0baro do you still know the rationale for setting the `DAL` `validators` and `validators_method` to `None` when a `NullAdapter` is created in https://github.com/web2py/pydal/commit/85c530cc791e2aadbbbf6302b2f58d354d9800b5?
@hrother nope, I can't remember, but I remember we did it for a specific reason. 
@gi0baro that's okay we can fix it on the web2py side by setting the validators_method after creating the DAL(None) instance in SQLFORM.factory.

Resolving patch:
diff --git a/gluon/sqlhtml.py b/gluon/sqlhtml.py
--- a/gluon/sqlhtml.py
+++ b/gluon/sqlhtml.py
@@ -2023,7 +2023,7 @@ def factory(*fields, **attributes):
         to hold the fields.
         """
         # this is here to avoid circular references
-        from gluon.dal import DAL
+        from gluon.dal import DAL, _default_validators
         # Define a table name, this way it can be logical to our CSS.
         # And if you switch from using SQLFORM to SQLFORM.factory
         # your same css definitions will still apply.
@@ -2036,8 +2036,9 @@ def factory(*fields, **attributes):
 
         # Clone fields, while passing tables straight through
         fields_with_clones = [f.clone() if isinstance(f, Field) else f for f in fields]
-
-        return SQLFORM(DAL(None).define_table(table_name, *fields_with_clones), **attributes)
+        dummy_dal = DAL(None)
+        dummy_dal.validators_method = lambda f: _default_validators(dummy_dal, f) # See https://github.com/web2py/web2py/issues/2007
+        return SQLFORM(dummy_dal.define_table(table_name, *fields_with_clones), **attributes)
 
     @staticmethod
     def build_query(fields, keywords):


Test patch: