[Grok-dev] Re: how to hide a field in setUpWidgets

Robert Marianski rmarianski at openplans.org
Thu Jul 10 12:02:46 EDT 2008


On Thu, Jul 10, 2008 at 05:18:28PM +0200, Ivo van der Wijk wrote:
> 2008/7/10 Ivo van der Wijk <vladdrac at gmail.com>:
> > Hi,
> >
> > In my add/edit forms I want to conditionally hide a field created
> > through grok.AutoFields and, if possible, provide a (default) value.
> > Removing the field entirely is also an option.
> >
> > Any hints on how to do this? There's a hidden() method on most widgets
> > that render the field hidden, but I don't know how I can get the
> > formrendering machinery to invoke that.

I've done something similar in a zope2 setting. My use case was to
display a contact form, and if the user was logged in, I didn't want to
display the name and email fields because I could pull that from the
member object. I created custom widgets and overrode the setupWidgets
method. Here's the code:
http://trac.openplans.org/livablestreets/browser/sputnik/trunk/sputnik/nui/main/contact.py#L95

-- 
Robert

> Answering my own question: With help from timte on #grok I've found
> the following solution:
> Conditionally set self.form_fields using grok.Autofields combined with
> omit, i.e.
> 
> def __init__(self, context, request):
>   super(...)(context, request)
>   if cond(self.request):
>     self.form_fields = grok.AutoFields(..).omit('special_field')
>   else:
>     self.form_fields = grok.AutoFields(..)
> 
> I'm still open to alternative suggestions (i.e. hiding the widget) but
> this will do the trick for now.
> 
> Regards
> 
> Ivo
> 
> 
> -- 
> Drs. I.R. van der Wijk / m3r Consultancy B.V.
> Linux/Python/Zope/Plone and Open Source solutions
> PO-box 51091, 1007 EB Amsterdam, The Netherlands
> Email: ivo <at> m3r.nl Web: http://m3r.eu/
> _______________________________________________
> 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