> 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. 
>

For any fields you don't want displayed, just set the readable and writable 
attributes to False. You can also specify the "fields" argument to SQLFORM 
to tell it exactly which fields to include.
 

> 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)
>

This can be checked either via an onvalidation function or prior to even 
creating/processing the form.
 

> , 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 can be done either with an onvalidation function or with two 
validators:

    requires = [IS_MATCH(...), IS_NOT_IN_DB(db([set filtered based on kind 
and budget_id]), ...)]
 

> This means that addCategoryForm.accepts() only does part of the validation.
>

This is not necessary. You are giving up all the automatic processing just 
to do some custom validation that you can already do with existing 
validation functionality.

Finally, why is create_category in a try..except? Can't you just have it 
return an error message rather than raise an exception (if you do raise an 
exception, it should be a specific one, so you don't mistakenly catch real 
errors in the code).

Anthony

-- 

--- 
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/groups/opt_out.


Reply via email to