There are several problems. First, note that by default, form.process() 
sets the flash message after processing, and that happens *after* the 
database insert -- so the flash message you set in the _before_insert 
callback gets overwritten.

Second, even though your callback prevents the insert, the form processing 
code has no way of knowing the insert failed, so form.process().accepted 
will still be true, and your explicit setting of response.flash to 
T('Record added') will still be made.

Instead of handling the validation via an insert callback, you should 
instead use the form validation mechanisms. In particular, your code could 
be greatly simplified by just adding an IS_NOT_IN_DB validator:


db.poc.Name.requires = [IS_UPPER 
<https://xortho.it/examples/global/vars/IS_UPPER>(), CRYPT 
<https://xortho.it/examples/global/vars/CRYPT>(),
                        IS_NOT_IN_DB(db(db.poc.Birthday == 
request.vars.Birthday), 'poc.Name')]


A few additional tips:


   - form.process() accepts a "message_onsuccess" argument, so you can use 
   that instead of checking if the form is accepted and explicitly setting 
   response.flash.
   - If your callback function already has the proper signature (i.e., 
   accepts the arguments that will be passed when called), there is no need to 
   wrap it in a lambda. Just do 
   db.poc._before_insert.append(controlla_record).
   - If you want to determine whether a given value exists in a particular 
   database field, you should not loop through all the records in Python but 
   instead simply let the database do the work:

if not db((db.poc.Birthday == dati.get("Birthday")) &
          (db.poc.Name == CRYPT()(request.vars.get('Name', 
'').upper())[0])).isempty():
    return 'error'


Anthony

On Thursday, February 18, 2016 at 12:13:55 PM UTC-5, Ivan Gazzola wrote:
>
> ### Controller for poc form
>
>
> def insert_poc():
>     form=SQLFORM(db.poc)
>     if form.process().accepted:
>        response.flash = T('Record added')
>     return dict(form=form)
>
>
> I've posted the full model, the function is colled in _before_insert:
>
> db.poc._before_insert.append(lambda dati: controlla_record(dati))
>
>
> Il giorno giovedì 18 febbraio 2016 17:28:15 UTC+1, Anthony ha scritto:
>
> We need to see the full code, including the controller. In particular, you 
> show a callback function, but you do not show where it is called.
>
> Anthony
>
> On Thursday, February 18, 2016 at 8:55:49 AM UTC-5, Ivan Gazzola wrote:
>
>
> db.define_table('poc',
>                 Field 
> <https://xortho.it/examples/global/vars/Field>('Name',required=True),
>                 Field 
> <https://xortho.it/examples/global/vars/Field>('Birthday','date',widget=bsdatepicker_widget(),required=True),
>                 Field 
> <https://xortho.it/examples/global/vars/Field>('Angle_Class_Dx',label=T 
> <https://xortho.it/examples/global/vars/T>("Angle's Class Dx")),
>                 Field 
> <https://xortho.it/examples/global/vars/Field>('Angle_Class_Sn',label=T 
> <https://xortho.it/examples/global/vars/T>("Angle's Class Sn")),
>                 Field 
> <https://xortho.it/examples/global/vars/Field>('Facial_Type'),
>                 Field 
> <https://xortho.it/examples/global/vars/Field>('Overjet'),
>                 Field 
> <https://xortho.it/examples/global/vars/Field>('Overbite'),
>                 Field <https://xortho.it/examples/global/vars/Field>('Sex'),
>                 Field 
> <https://xortho.it/examples/global/vars/Field>('Ratial'),
>                 Field 
> <https://xortho.it/examples/global/vars/Field>('Cot1','upload',required=True),
>                 Field 
> <https://xortho.it/examples/global/vars/Field>('Cot2','upload'),
>                 Field 
> <https://xortho.it/examples/global/vars/Field>('Cot3','upload'),
>                 Field 
> <https://xortho.it/examples/global/vars/Field>('Clench','upload',required=True),
>                 Field 
> <https://xortho.it/examples/global/vars/Field>('Chewing_dx','upload',required=True),
>                 Field 
> <https://xortho.it/examples/global/vars/Field>('Chewing_sn','upload',required=True),
>                 Field 
> <https://xortho.it/examples/global/vars/Field>('Lateral_Ceph','upload'),
>                 Field 
> <https://xortho.it/examples/global/vars/Field>('Dental_Formula','text'),
>                 auth.signature
>                 )
>
>
>
> def controlla_record(dati):
>     nascite=db(db.poc.Birthday==dati.get("Birthday")).select()
>     for soggetto in nascite:
>         if CRYPT <https://xortho.it/examples/global/vars/CRYPT>()(request 
> <https://xortho.it/examples/global/vars/request>.vars.get("Name",'').upper())[0]==soggetto.Name:
>           response.flash=T("Duplicate Records Not Allowed") 
>           return "errore"
>
>
> db.poc.Angle_Class_Dx.requires = IS_IN_SET 
> <https://xortho.it/examples/global/vars/IS_IN_SET>([('1','I'),('2','II'),('3','III')])
> db.poc.Angle_Class_Sn.requires = IS_IN_SET 
> <https://xortho.it/examples/global/vars/IS_IN_SET>([('1','I'),('2','II'),('3','III')])
> db.poc.Facial_Type.requires=IS_IN_SET 
> <https://xortho.it/examples/global/vars/IS_IN_SET>({'N':'Normo','O':'Open','D':'Deep'})
> db.poc.Overjet.requires=IS_IN_SET 
> <https://xortho.it/examples/global/vars/IS_IN_SET>([('1','OJ < 
> 2mm'),('2','2mm < OJ < 4mm'),('3','OJ > 4mm')])
> db.poc.Overbite.requires=IS_IN_SET 
> <https://xortho.it/examples/global/vars/IS_IN_SET>([('1','OB < 
> 2mm'),('2','2mm < OB < 4mm'),('3','OB > 4mm')])
> db.poc.Sex.requires= <https://xortho.it/examples/global/vars/IS_IN_SET>
>
> ...

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to