Here is an example form created and handled in the controller's index() 
function:

addCategoryForm = FORM('Add category:',
        INPUT(_name='category', requires=IS_NOT_EMPTY()),
        INPUT(_type='submit'))
    if addCategoryForm.accepts(request, session, formname='addCategory'):
        try:
            create_category(request.vars.category, auth.user.budget_id, 
auth.user.id)
            redirect(URL())
        except Exception, e:
            addCategoryForm.errors['category'] = e.message

I don't like using the SQLFORM object because the category table contains a 
budget_id, a kind (since I have more than one type of category), and an 
isActive boolean, all of which are either set to a default value or depend 
on the user that is logged in. The db function create_category() verifies 
that the user has permission to edit that budget (no, auth_groups don't 
work for me in this case), and it performs validation on the name by 
checking for unwanted characters and making sure that the name is unique 
for that specific combination of kind and budget_id. Thus the typical 
'requires=unique' or whatever it is when defining a table is insufficient.

This means that addCategoryForm.accepts() only does part of the validation. 
If it passes the create_category() function without error, then it is truly 
successful and I can redirect the url to avoid the stupid resubmission of a 
post that results from a page refresh. I would like to be able to also 
avoid the post resubmission after an invalid post. I could put a 
redirect(URL()) in the code no matter what, but then the error messages get 
erased.

My problem stated in my last post to this topic, that of having the error 
messages not go away, turned out to be because of something else I was 
doing wrong, which is now fixed, so that is actually not related much.

-- 

--- 
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 web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to