[Grok-dev] unique item in container question

Christian Klinger cklinger at novareto.de
Tue Mar 17 07:38:55 EDT 2009


Hi again,

another option is to use the validator on your grok.action.

Look at this little snippet maybe it helps you.


##################################################

     def simple_validator(self, action, data):
         errors = self.validate(action, data)
         if object already there:
             errors = (u"Entry already exists",)
         return errors

     @grok.action(u'Anlegen', validator=simple_validator)

##################################################

Christian

> Hi Christian,
> 
> Thank you for the suggestion it is very helpful.
> 
> I still have to find a way to make the form report the error. I that  
> this issue is grok related.
> 
> My problem becomes when the NameChoser rises a ValueError how can I  
> make the form report the error to the user?
> 
> At this moment if i do this :
> 
>      @grok.action('Add event')
>      def add(self,**data):
>          .....
>          if self.context.has_key(name):
>              raise ValueError("some error")
> It will report the exception in zope error log. I need to find a way  
> to inform the user about the problem so he can change the name (in the  
> context of this application this is how we must handle the situation)
> 
> Thank again
> 
> 
> 
> On Mar 17, 2009, at 11:53 AM, Christian Klinger wrote:
> 
>> Hi modiadi,
>>
>> in zope3 there is a concept called NamedChooser. You can look here [1]
>> for the default implementation. Of course you can override the
>> NameChooser for your NewsContainer so you can check if the news item
>> already exists.
>>
>> There is an example in Philipp´s *Web Component Development with  
>> Zope3*.
>>
>> Hope this helps
>> - Christian
>>
>> [1]
>> http://svn.zope.org/zope.container/trunk/src/zope/container/contained.py?rev=97508&view=auto
>>
>>
>>> Hello,
>>>
>>> I have a grok.AddForm wich adds a NewsItem in a NewsContainer. I try
>>> to write some kind of validation so I dont have duplicate news in the
>>> container.
>>>
>>> As I see I have 3 a few options:
>>>
>>> 1. Write an invariant in the NewsItem model where I check if the news
>>> is unique in the *container*. I don't see this as a good option
>>> because I have to explicitly refer to the container in side a model.
>>>
>>> 2. Write the validation in the Add grok.action. The idea is that just
>>> before adding the new item in the container I check if I have
>>> duplicates. This was easy to do.
>>>
>>> My problem is that I can't(or I don't know how to) return an error to
>>> the user from this place.
>>>
>>>     @grok.action('Add event')
>>>       def add(self,**data):
>>>           ...... do some validation
>>> 	  return None?
>>>
>>> Do you have any suggestions ?
>>>
>>> Thank you !
>> _______________________________________________
>> Grok-dev mailing list
>> Grok-dev at zope.org
>> http://mail.zope.org/mailman/listinfo/grok-dev
>>



More information about the Grok-dev mailing list