Massimo,
Thank you.
As always, if I'm doing something wrong I would be delighted to know
it.
>From the limited checking I have been able to do, I think the problem
is in the SELECT helper, not in the validator.
Anyway, here's the code.
## model
db.define_table('buckslip_types',
Field('name', length=32),
)
db.define_table('buckslips',
Field('type', db.buckslip_types),
Field('status', 'list:string'),
Field('date_received', 'datetime'),
Field('date_approved', 'datetime'),
Field('date_posted', 'datetime'),
Field('comments', 'text'),
Field('document_filename', length=256),
Field('document','upload')
)
db.buckslips.type.requires = IS_IN_DB(db, 'buckslip_types.id',
'%(name)s', zero=None)
db.buckslips.status.requires = IS_IN_SET(['received', 'approved',
'disapproved', 'posted'],
multiple=False
)
## controller buckslips.py
def edit():
record = db.buckslips(request.args[-1]) or redirect(URL('index'))
url = URL('download')
db.buckslips.document.writable = False
# db.buckslips.status.widget = my_select_widget
fields=['date_received', 'status', 'document', 'comments']
form = SQLFORM(db.buckslips, record, deletable=False,
upload=url, fields=fields,
)
if request.vars.document!=None:
form.vars.document_filename= request.vars.document.filename
if form.process().accepted:
response.flash = 'form accepted'
elif form.errors:
response.flash = 'form has errors'
return dict(form=form)
On Mar 1, 11:26 am, Massimo Di Pierro <[email protected]>
wrote:
> Can you provide an example of IS_IN_SET not working so we can fix it? By
> the way, there was a change in IS_IN_SET in trunk and that may have fixed
> your problem.
>
>
>
>
>
>
>
> On Thursday, 1 March 2012 09:05:51 UTC-6, Cliff wrote:
>
> > Running 1.99.4
>
> > I have not been able to get the IS_IN_SET validator to work properly.
> > I don't know why this should be.
>
> > When used with SQLFORM to edit a record, the select field refuses to
> > show the current value of the field. Instead it shows the first value
> > in the list or the zero value for the field.
>
> > So I made an alternative widget. The code consists of two functions,
> > below.
>
> > If you are having similar problems, feel free to use it.
>
> > The _style definition at the end of the call is there to make sure I
> > was seeing my widget instead of the default. It is not necessary for
> > the widget to work.
>
> > ############################################################
>
> > def is_it_selected(current_value, option, multiple):
> > if len(current_value) == 1 or not multiple:
> > current_value=current_value[0]
> > if current_value==option:
> > out = 'selected'
> > else:
> > out = None
> > else:
> > if option in current_value:
> > out='selected'
> > else:
> > out=None
> > return out
>
> > def my_select_widget(field,value):
> > # diagnostics uncomment to learn
> > #print 'value'
> > #print value
> > #print 'type(value)'
> > #print type(value)
>
> > f = str(field)
> > f_name = f.split('.')[-1]
> > f_id = f.replace('.', '_')
> > f_requires = db[request.controller][f_name].requires
> > the_set = f_requires.theset
> > the_multiple = f_requires.multiple
> > option_set = []
> > for t in the_set:
> > is_selected = is_it_selected(value, t, the_multiple)
> > option_set.append(OPTION(t, _selected = is_selected))
> > return SELECT(option_set, _name='status', _id='buckslips_status',
> > _style='color:red;'
> > )