Hi Massimo,

How to add a IS_NOT_EMPTY constrait to list:reference field? This
seems a simple task but I ends up opening a can of worms. :-/  Here is
what I did.

1. Firstly, setup the sandbox.

    db.define_table('tag',
        Field('name', requires=IS_NOT_EMPTY()), format='%(name)s')
    db.define_table('product',
        Field('name', requires=IS_NOT_EMPTY()),
        Field('tags', 'list:reference tag'),)

    def product():
        return {
            1: crud.update(db.product, request.args(0)),
            2: crud.select(db.product),
            }

Now we can add new product with or without tags. Fine.


2. What if we need new product to have at least one tag?

I tried changing the tags field definition into (I thought it was
intuitive):

    Field('tags', 'list:reference tag', required=True)

But nothing different. I can't force a tag.


3. Then I tried to change tags into:

    Field('tags','list:reference tag',
        requires =
IS_IN_DB(db,'tag.id',db.tag._format,multiple=True),),

This is the so-called default constraint, according to the book,
chapter 6.
But situation gets worse. Now I get error ticket whenever submitting
new product.


4. I even tried:

    requires = [IS_NOT_EMPTY(), IS_IN_DB(...)]

still tickets.


Problem persists in both web2py 1.95.1 and 1.97.1, on Windows.

Did I miss something really obvious? Thanks in advance.


Regards,
Ray (a.k.a. Iceberg)

Reply via email to