Hi Fran,
list:integer field can not contain nulls but you can work around it, here
is an example:
# model
def integer_null(x):
if not x:
return x
w=[]
for v in x:
if v==9999:
w.append('')
else:
w.append(v)
return w
db.define_table("test",
Field("text", requires=IS_NOT_EMPTY()),
Field("membership_paid", "list:integer", label = T("Membership Paid"),
requires = IS_LIST_OF(IS_IN_SET([2010,2012],multiple=True)),
),
)
db.test.membership_paid.widget=lambda field,value: \
SQLFORM.widgets.list.widget(field,integer_null(value))
# controller
def null_integer(form):
w = []
for v in form.vars.membership_paid:
if v:
x=v[0]
else:
x='9999'
w.append(x)
form.vars.membership_paid = w
def index():
form = SQLFORM(db.test)
if form.process(onvalidation=null_integer).accepted:
response.flash = 'form accepted'
elif form.errors:
response.flash = 'form has errors'
else:
response.flash = 'please fill out the form'
return dict(form=form)
The code just converts null into 9999, so it can be stored as in integer.
You can use any other integer value that will never be a part of the set.
Denes
On Friday, March 30, 2012 1:07:08 PM UTC-4, Fran wrote:
>
> On Friday, 30 March 2012 17:42:45 UTC+1, Fran wrote:
>>
>> On Friday, 30 March 2012 17:01:43 UTC+1, Anthony wrote:
>>>
>>> I see it did default to 0, but the form doesn't get saved...just as I
>>>>> originally said...
>>>>>
>>>> The error comes from the e in (v, e) = self.other(item)
>>>> i.e. "value not allowed" from the IS_IN_SET()
>>>> I tried adding "" to the set, but obviously that's not an integer so it
>>>> gets rejected...
>>>>
>>> Is the problem that one or more of the input fields generated by the
>>> widget is being submitted with no value, and therefore the IS_IN_SET
>>> validator is returning an error?
>>>
>> Exactly
>>
> Do you not want an error in that case?
>>
> Right
>>
>
> I thought that I might eb able to get away with just changing IS_LIST_OF
> to:
> if self.minimum and self.other:
> (i.e. don't bother checking the other if minimum=0 since this will be an
> IS_NULL_OR()
>
> However this gives the same basic error:
>
> File "C:\Bin\web2py\gluon\sqlhtml.py", line 1290, in accepts
> self.vars.id = self.table.insert(**fields)
> File "C:\Bin\web2py\gluon\dal.py", line 7030, in insert
> ret = self._db._adapter.insert(self,self._listify(fields))
> File "C:\Bin\web2py\gluon\dal.py", line 968, in insert
> query = self._insert(table,fields)
> File "C:\Bin\web2py\gluon\dal.py", line 964, in _insert
> values = ','.join(self.expand(v,f.type) for f,v in fields)
> File "C:\Bin\web2py\gluon\dal.py", line 964, in <genexpr>
> values = ','.join(self.expand(v,f.type) for f,v in fields)
> File "C:\Bin\web2py\gluon\dal.py", line 1100, in expand
> return str(self.represent(expression,field_type))
> File "C:\Bin\web2py\gluon\dal.py", line 1456, in represent
> obj = [int(item) for item in obj]
> ValueError: invalid literal for int() with base 10: ''
>
>
> Can list:integer fields not be NULL?
>
> F
>