Hi Annet,

when you use SQLFORM to update a record you supply the record or the
record id, so web2py can retrieve it for you. The IS_NOT_IN_DB
validator checks against the record id so updates don't get a
validation error.

Since SQLFORM.factory does not allow you to supply the record or
record id it can not be used as is. But you can modify the validator
before the accepts to skip the error for the particular record you are
using, as shown below. Note: AFAIK this is undocumented, so it may
change in the future.

On Mar 7, 4:17 am, annet <annet.verm...@gmail.com> wrote:
> In db.py I defined the following table:
>
> db.define_table('company',
>     Field('name',length=54,default='',notnull=True),
>     Field('CoC_number',length=8),
>     Field('subdossiernumber',length=4,default='0000'),
>     ...
>     Field(...),
>     migrate=False)
>
> db.company.CoC_number.requires=IS_NOT_IN_DB(db(db.company.subdossiernumber==request.vars.subdossiernumber),db.company.CoC_number,error_message='combination
> of CoC-number and subdossiernumber already in database')
>
> In a controller I defined one form to update multiple tables. Here are
> the parts related to the company table:
>
> def update():
>     id= request.args(0)
>     ### retrieve company data
>     company=db(db.company.id==id).select(db.company.ALL)
>     ### create form
>     form=SQLFORM.factory(db.bedrijf,...,...)
>     ### pre-populate form
>     if company:

          # this is undocumented
          db.company.CoC_number.requires.set_self_id(company[0].id)

>         form.vars.name=company[0].name
>         form.vars.CoC_number=company[0].CoC_number
>         form.vars.subdossiernumber=company[0].subdossiernumber
>         ...
>         if form.accepts(request.vars,session):
>
> company.update_record(**db.bedrijf._filter_fields(form.vars))

  # this should be:
  company[0].update_record(**db.company._filter_fields(form.vars))

>            ...
>             session.flash='Records updated'
>             redirect(URL(r=request,f='retrieve',args=id))
>         elif form.errors:
>             response.flash=response.flash_formerror
>         return dict(form=form)
>
> When I execute the function the data is retrieved, the form is created
> and pre-populated, however, when I click the submit button, the
> validator on the Coc_number and subdossiernumber field prevents the
> record from being updated and displays the error_message: 'combination
> of CoC-number and subdossiernumber already in database'
>
> I didn't expect this to happen when updating a record without changing
> the CoC-number and subdossiernumber, I'd expect this to happen when I
> change the CoC-number and subdossiernumber of one company to that of
> another company. Is there a solution to solve this problem?
>
> Kind regards,
>
> Annet.

Reply via email to