Hi Miguel, contact_set=dict([(contact.id,contact.name) for contact in contacts])
and in your controller override your requires: db.task.contact_id.requires=IS_EMPTY_OR(IS_IN_SET (contact_set,zero=None)) note that you can use SQLFORM too here: form=SQLFORM(db.task,fields=['contact_id',...]) fields=[list_of_field_names_that_should_be_in_the_form] and form.accepts will validate using the above requires if it is set before creating the form in the controller. Denes On Jan 15, 9:23 am, Miguel Lopes <[email protected]> wrote: > On Fri, Jan 15, 2010 at 2:00 PM, DenesL <[email protected]> wrote: > > > Does the error occur on the 'empty' option of the select?. > > You are introducing a 0 index value which is invalid in the db > > Yes. You are right! > But how can I have a select with an empty option. > > Please note the following extra info: > > The select should display a subset of db.contact records (those that are > related to the account that is the parent of the account). So in the > controller function I pass the contacts set to the view: > > # Task form > form2=_frmTaskAdd(account_id, opportunity_id, frmName="form2", > fields=['title', 'task_type', 'contact_id', > 'description']) # just a reusable SQLFORM > > contacts=db(db.contact.account_id==account_id).select() > return dict(opportunity=opportunity, form=form, form2=form2, > tasks=tasks, quotes=quotes, contacts=contacts) > > I the view in a custom form the select is constructed. This might very well > be the source of the problem. But How should I them allow for no contact > selection? > > {{keys, values=[''],[0] > keys.extend([contact.name for contact in contacts]) > values.extend([contact.id for contact in contacts]) > items=zip(values,keys)}} > {{=SELECT([OPTION(k,_value=v) for (v,k) in items], _name="contact_id")}} > > The model: > db.define_table('task', > Field('title', label='Actividade'), > ... > Field('contact_id',db.contact,default=None, label='Contacto'), > ...) > > db.task.contact_id.requires=IS_IN_DB(db,'contact.id','%(name)s') > db.task.contact_id.requires.zero='' > > Miguel
-- You received this message because you are subscribed to the Google Groups "web2py-users" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/web2py?hl=en.

