Gustavo Narea wrote:
> Hello,
>
> I have a widget which already has its relevant validators, but in my 
> controller I'd like to invalidate it if it's trying to add an existing record.
>
> The following code snipped partially works and seems like an ugly solution to 
> me:
>   
>>     @expose('animador.web.templates.websites.admin.add')
>>     def add(self, **kwargs):
>>         if pylons.tmpl_context.form_errors:
>>             flash(_("Please correct the form"))
>>         pylons.c.add_site_form = widgets.AddWebsiteForm("add_site",
>>                                                      action="add_website") 
>>         return dict()
>>
>>     @expose('animador.web.templates.websites.admin.add')
>>     @validate(form=widgets.AddWebsiteForm(), error_handler=add)
>>     def add_website(self, website_alias):
>>         if Website.query().get(website_alias):
>>             error_msg = _(u'The web site alias "%s" is already in use') % \
>>                         website_alias
>>             pylons.tmpl_context.form_errors['website_alias'] = error_msg
>>             return self.add()
>>         new_site = Website()
>>         new_site.website_alias = website_alias
>>         new_site.created = datetime.now()
>>         DBSession.save(new_site)
>>         DBSession.commit()
>>         flash(_('The "%s" web site has been added!') % website_alias)
>>         return dict()
>>     
>
> I want add_website() to reject existing websites, which is done with the code 
> above, but the error message I inserted in pylons.tmpl_context.form_errors is 
> not displayed (not even if I redirect() to the add() action, instead of 
> returning a call to it).
>   

You need to pass the form_errors to the form explicitly if you want to
build them yourself:

${form.display(...., error=c.form_errors or None)}
> How can I do this the right way?
>   
I would check if the website alias is unique in a custom validator
(inside the validate_python method) for the website alias filed, not the
controller. This way the error will be displayed beside the website
alias field and the validate decorator can take care of everything.

Alberto

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"TurboGears Trunk" 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-trunk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to