On Aug 13, 2006, at 4:17 AM, Ian Wilson wrote:

>
> Hello,
>   How can I emulate the error handler without using a validator?  For
> example if a product's name is a duplicate I need to go back to the
> form and have name's error text read "A product with this name already
> exists."
>
> So say I have something like this:
>
> @expose(...)
> def edit(self, **kwargs):
>   return dict(form=form, ...)
>
> @expose(...)
> @validate(form=form)
> @error_handler(edit)
> def save(self, **kwargs):
>   id = kwargs.get('id', None)
>   name = kwargs.get('name')
>   #Check if duplicate exists
>   try:
>     duplicateProduct = Product.byName(name):
>   except:
>     pass
>   else:
>     if duplicateProduct.id != id: #Product with same name and
> different id exists
>       #Error here for name field, what goes here to get back to edit?
>       ...
>
>   ...
>
>   redirect("/edit", id=id)

You could check for the duplicate in a custom validator for the name  
field so you can raise an Invalid exception with the error message. I  
think it would be the easiest way to accomplish what your'e trying to  
do.

Alternatively, you could raise an exception insde the controller and  
catch it with an exception_handler that branches to an exception  
handler method.

HTH,
Alberto



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"TurboGears" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/turbogears
-~----------~----~----~----~------~----~------~--~---

Reply via email to