web2py-sqlform can't check unique=True that use with requires=IS_LENGTH()

sqlform don't show error message when data have same value it accepted then error appear error1 detail ps. my goal is to create a field that contain 13 figure number which not same as other

i try delete requires=IS_LENGTH(maxsize=13,minsize=13) then the sqlform work fine but which these method i can't check either string is equal 13 or not

db.define_table('person',
Field('h_id_card',unique=True,requires=IS_LENGTH(maxsize=13,minsize=13))
)
def add():
form = SQLFORM(db.person).process()
    return locals()

i expected sqlform will show error message instead of accepted

this is what i expect

Upvotes: 0

Views: 42

Answers (1)

Anthony
Anthony

Reputation: 25536

From the book:

Notice that requires=... is enforced at the level of forms, required=True is enforced at the level of the DAL (insert), while notnull, unique and ondelete are enforced at the level of the database. While they sometimes may seem redundant, it is important to maintain the distinction when programming with the DAL.

Because unique=True translates to the UNIQUE SQL statement, when an insert/update violates the uniqueness constraint, you simply get an error from the database, which generates an exception in the database driver, which ultimately generates an exception in your app code if you don't catch it.

If you instead want to enable form validation for the uniqueness requirement, you should use the IS_NOT_IN_DB validator:

Field('h_id_card',
      requires=[IS_LENGTH(maxsize=13, minsize=13), IS_NOT_IN_DB(db, 'person.h_id_card')])

Upvotes: 1

Related Questions