allow NULL in date field in SQLFORM

144 views
Skip to first unread message

Horst Herb

unread,
May 22, 2009, 4:37:48 AM5/22/09
to web2py
given a table with a 'date' field
and the crud generated or SQLFORM generated forms,
how can I tell the form to accept *empty* values for a date?

Thanks,
Horst

annet....@gmail.com

unread,
May 22, 2009, 5:20:47 AM5/22/09
to web2py Web Framework
Horst,


In the table definition:

SQLField('fieldname', type='date')

... and a validator on this field which reads like:

db.tablename.fieldname.requires=IS_NULL_OR(IS_DATE(str(T('%Y-%m-
%d'))))



Kind regards,

Annet.

Horst Herb

unread,
May 22, 2009, 5:51:11 AM5/22/09
to web...@googlegroups.com
On Fri, May 22, 2009 at 7:20 PM, <annet....@gmail.com> wrote:
> db.tablename.fieldname.requires=IS_NULL_OR(IS_DATE(str(T('%Y-%m-
> %d'))))

Thanks, that was what I was looking for!

Another question: how / where can I (application wide) change the date
format string so that all date entry and display conforms to it incl
the date pickers? (eg '%d.%m.%Y')

Horst

Message has been deleted

annet....@gmail.com

unread,
May 23, 2009, 2:47:03 AM5/23/09
to web2py Web Framework
Horst,

Model:

T.force('en-de') ## en-de is a language file you'll create below


In the table definition:

SQLField('fieldname', type='date')


... a validator on this field which reads like:

db.tablename.fieldname.requires=IS_NULL_OR(IS_DATE(str(T('%Y-%m-
%d'))))


Languages:

... create a language file e.g. en-de.py and edit the dates:

Original Translation
%Y-%m-%d %d-%m-%Y
%Y-%m-%d %H:%M:%S %d-%m-%Y %H:%M:%S

Click update.


Views:

... to use the date in a view:

{{=db.tablename.fieldname.formatter(tablename.fieldname)}}


If you use form_factory to create a custom form, the code in the
controller should read:

SQLField('fieldname',type='date',requires=IS_DATE(str(T('%Y-%m-
%d'))))


I hope I did not leave anything out, if so, let me know.


Kind regards,
Annet.

Horst Herb

unread,
May 23, 2009, 3:53:14 AM5/23/09
to web...@googlegroups.com
On Sat, May 23, 2009 at 4:47 PM, <annet....@gmail.com> wrote:
> T.force('en-de')  ## en-de is a language file you'll create below
>
>
> In the table definition:
>
> SQLField('fieldname', type='date')
>
>
> ... a validator on this field which reads like:
>
> db.tablename.fieldname.requires=IS_NULL_OR(IS_DATE(str(T('%Y-%m-
> %d'))))
>
>
> Languages:
>
> ... create a language file e.g. en-de.py and edit the dates:
>
> Original                                Translation
> %Y-%m-%d                                %d-%m-%Y
> %Y-%m-%d %H:%M:%S               %d-%m-%Y %H:%M:%S

Thanks a million!

Horst

DenisBY

unread,
Jul 21, 2009, 10:16:44 AM7/21/09
to web2py-users
Hello

How I should define format if my controller looks like:
def view():
form = SQLFORM(db.book)
records = db(db.book.id>0).select(orderby='date')
if form.accepts(request.vars):
redirect(URL(r=request,f='view'))
if form.errors:
response.flash="error"
return dict(form=form,records=records)

and in view:
{{for record in records:}}
<tr id="record-{{=record.id}}" class="record">
<td>{{=record.nn}}</td>
<td>{{=record.name}}</td>
<td>{{=record.num}}</td>
<td>{{=record.date}}</td>
<td>{{=record.operation}}</td>
<td>{{=record.inc_sale}}</td>
<td>{{=record.inc_copy}}</td>
<td>{{=record.inc_other}}</td>
<td>{{=int(record.inc_sale)+int(record.inc_copy)+int
(record.inc_other)}}</td>
<td><a href='{{=URL(r=request,f="d",args=[record.id])}}'
class="delete" >X</a>
</tr>
{{pass}}

in input field date looks like I defined it (%d.%m.%Y), but after
insertion, in view, it looks like defaul (%Y-%m-%d)

mdipierro

unread,
Jul 21, 2009, 6:18:11 PM7/21/09
to web2py-users
if you change the format of IS_DATE() you also need to change the
format for calendar.js. This is done in web2py_ajax.html.

Alternatively you can leave IS_DATE and web2py_ajax.html as they were
and translate both strings using internationalization.

Massimo

DenisBY

unread,
Jul 22, 2009, 9:22:28 AM7/22/09
to web2py-users
I've translated it so translation looks like:
%Y-%m-%d => %d.%m.%Y
in view in input field date looks like %d.%m.%Y but after adding it
becomes %Y-%m-%d
you can test it here:
http://84.201.238.210:8000/buh/book/view

mdipierro

unread,
Jul 22, 2009, 5:12:53 PM7/22/09
to web2py-users
do not use

{{=row.datefield}}

use

{{=db.table.datetfield.formatter(row.datefield)}}

DenisBY

unread,
Jul 23, 2009, 10:50:11 AM7/23/09
to web2py-users
thanks! it works!

On 23 июл, 00:12, mdipierro <mdipie...@cs.depaul.edu> wrote:
> do not use
>
> {{=row.datefield}}
>
> use
>
> {{=db.table.datetfield.formatter(row.datefield)}}
>
> On Jul 22, 8:22 am, DenisBY <denis....@gmail.com> wrote:
>
> > I've translated it so translation looks like:
> > %Y-%m-%d=> %d.%m.%Y
> > in view in input field date looks like %d.%m.%Ybut after adding it
> > > > > %Y-%m-%d%H:%M:%S               %d-%m-%Y%H:%M:%S
Reply all
Reply to author
Forward
0 new messages