Q:

Web2py CRUD.read() record created though non-filled condition

So i worte a function in Web2py to create a record into a table in the Database under a Condition, but Web2py create the record allthough that condition is not filled, 
here is the function
def buy_product():
    price = price_set(db,auth,'product')
    balance = limit(db,auth,'settings','account_balance')
    if balance !=None:
        if balance < price:
            form=redirect(URL('order'))
        else:
            form=crud.create(db.letter)
            if form.accepts(request.vars, session):
                tax = float(postage(form.vars.tax_type).replace("-","."))
                ##########################
                # I'm talking about this #
                ##########################
                if balance < (price + tax):
                    response.flash='You don\'t have enough balance to buy this product'
                    redirect(URL('not_processed'))
                else:
                    function_1(....)
                    ...
                    ...
                    update_field(db,auth,'settings','account_balance',-price)
                    response.flash='Done'
                    redirect(URL('products'))
                    pass
            elif form.errors:
                response.flash='Error 01'
            else:
                pass
            ###############################
    else:
        form=redirect(URL('settings'))
    return dict(form=form)

it's sepposed that when the Balance < price + tax the user should be redirected to not_processed without creating new record in the database.
but web2py redirect the user to not_processed and create the record without executing this part with the entered information from the user. so the user see that he bought something, when it has not processed (see below)            
        function_1(....)
        ...
        ...
        update_field(db,auth,'settings','account_balance',-price)
        response.flash='Done'
        redirect(URL('products'))
        pass

any idea ??
thank you

A:

Crud manages inserts/updates internally and it does not use the form.accepts.
You have two options:
1 - Use SQLFORM
    form=SQLFORM(db.letter)
    if form.accepts(request.vars, session):
        tax = float(postage(form.vars.tax_type).replace("-","."))
        ##########################
        # I'm talking about this #
        ##########################

2 - Use crud events
def myfunction(form):
    # do your stuff here
    # it will be called only when form is accepted

def myotherfunction(form):
    if form.errors:
        #do something here in case of errors
        #it will be called during the form validation

crud.settings.create_onvalidation = myotherfunction
crud.settings.create_onaccept = myfunction
#the above can be:
#crud.create_onaccept = lambda form: myfunction(form)

# define the above events before the creation of the form
form=crud.create(db.letter)

Note that SQLFORM is a bit different of crud, SQLFORM expects you to validate the form in form.accepts method, while crud does it internally and uses events as onvalidation, onaccept to custom validations.

