Hi Charlie

I have done something similiar with formish.

I extended the formish schema (structure) to take a context on
instantiation, and then apply (create any custom widgets
validators) just before rendering in the form handler.

You can see an early approach at
http://code.google.com/p/bfg-pages/source/browse/trunk/pages/schema.py#84
for the schema and
form handler at
http://code.google.com/p/bfg-pages/source/browse/trunk/pages/views.py#106

Cheers

Tim


On Tue, Sep 7, 2010 at 9:09 PM, Charlie Clark
<charlie.cl...@clark-consulting.eu> wrote:
> Am 07.09.2010, 06:56 Uhr, schrieb Tim Hoffman <zutes...@gmail.com>:
>
>> So in my contrived example I would like the set of possible values
>> for color is dependent on the
>> user and some other factor.  Looking at the code for SelectWidget and
>> RadioChoice widget
>> it appears they won't take a callable and lazily render those values
>> at render time.
>
> Tim,
>
> do you mean in a similar way to the way a vocabulary works in zope.schema?
> I discussed this a while back with Chris and the reason why this often
> extremely useful feature isn't built into deform is that deform on its own
> has no context. I have written something similar - a callable that works
> both as a validator for Schema nodes and as values for widgets:
>
>
> class Source(object):
>     """Utility for vocabulary constraints"""
>
>     def __init__(self, values):
>         """Can be initialised from a dictionary, a list of tuples
>         or a list"""
>         if hasattr(values, 'items'):
>             self.values = values.items()
>         elif hasattr(values, 'append'):
>             try:
>                 dict(values)
>                 self.values = values
>             except ValueError:
>                 self.values = enumerate(values)
>
>     def __call__(self, value):
>         return value in self.values
>
>     def __iter__(self):
>         return self.values.__iter__()
>
>
> colours = Source(
>                ['red, blue, green']
>                 )
>
>
> class MySchema(MappingSchema):
>
>
>       colour = SchemaNode(
>                 String(),
>                 title=u'Travel details',
>                 validator=colours)
>
> class Form(object):
>
>     def __init__(self, context, request):
>         self.context = context
>         self.request = request
>
>     form = Form(MySchema(), buttons=(u'submit',))
>     form['lift'].widget = widget.RadioChoiceWidget(values=colours)
>
>     def __call__(self):
>         html = self.form.render()
>         return {'form':html}
>
> I don't know how much of this could be truly dynamic.
>
> Charlie
> --
> Charlie Clark
> Managing Director
> Clark Consulting & Research
> German Office
> Helmholtzstr. 20
> Düsseldorf
> D- 40215
> Tel: +49-211-600-3657
> Mobile: +49-178-782-6226
> _______________________________________________
> Repoze-dev mailing list
> Repoze-dev@lists.repoze.org
> http://lists.repoze.org/listinfo/repoze-dev
>
_______________________________________________
Repoze-dev mailing list
Repoze-dev@lists.repoze.org
http://lists.repoze.org/listinfo/repoze-dev

Reply via email to