[Grok-dev] validators

Sebastian Ware sebastian at urbantalk.se
Wed Aug 12 07:26:41 EDT 2009


You can override validation behaviour in your edit/add view (excuse  
the hackish implementation, but I think you get the point):

     def validate(self, action, data):
         errors = grok.AddForm.validate(self, action, data)

         if not self.request.form.has_key('upfile') or  
type(self.request.form['upfile']) is unicode:
             e = WidgetInputError('upfile', 'Page image')
             errors.append(e)
             self.upfile_error = 'Required input is missing.'
         if not self.request.form.has_key('upmovie') or  
type(self.request.form['upmovie']) is unicode:
             e = WidgetInputError('upfile', 'Page movie')
             errors.append(e)
             self.upmovie_error = 'Required input is missing.'

         return errors

Another route, I believe, is to manually extend the list of fields  
with a schema definition for the drop down. I am not 100% what this  
would look like, but drop into the debugger and mess around with

   self.form_fields = grok.AutoFields(IMySchema)
   self.form_fields += grok.Fields(schema.Choice(bla bla...))

I am sure you can find something like it in the doctests.

Mvh Sebastian

12 aug 2009 kl. 11.22 skrev Thomas Bellembois:

> Hello Sebastian,
>
> Actually the field I want to validate is *not* one of the 4 fields  
> of my Molecule class.
> I have a 1->* relationship between the "Molecule" container and a  
> "Family" container.
> So I have modified the default Molecule homastemplate file to add a  
> drop down list with the families.
> I want to validate this drop down list (make it required).
>
> Thanks for your help.
>
> Regards,
>
> Thomas
>
> On Tue, Aug 11, 2009 at 1:35 PM, Sebastian Ware <sebastian at urbantalk.se 
> > wrote:
> You would normally do this in the interface and have the edit/add  
> views enforce the data integrity. If this solves your problem you  
> could look at this document:
>
> http://grok.zope.org/documentation/tutorial/work-with-forms
>
> Mvh Sebastian
>
> 10 aug 2009 kl. 18.47 skrev Thomas Bellembois:
>
> Hello,
>
> Where can we find a documentation about validators ? How can we  
> create a custom validator to overide the default validator ?
> I have a container "Molecule" with 4 fields:
>
> class Molecule(grok.Container):
>    interface.implements(IMolecule)
>    cas_number = u''
>    ce_number = u''
>    remark = u'--'
>
> and I have added a drop down list in the default template file to  
> add another field (like a foreign key). I would like this drop down  
> list to be a required field.
>
> Thanks for your help.
>
> Thomas
> _______________________________________________
> Grok-dev mailing list
> Grok-dev at zope.org
> http://mail.zope.org/mailman/listinfo/grok-dev
>
>
> _______________________________________________
> 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