On Mon, Jun 25, 2012 at 5:55 PM, Christoph Zwerschke <[email protected]> wrote:
> Here is a typical use case: How would you inject an options list dynamically
> in TW2? In our movie database example, assume that the "genre" options list
> depends on the user/request.

A possible solution is to use prepare itself, this would work both
when validating and when display the form by itself:

class MovieForm(TableForm):
    genre_options = [x for x in enumerate((
       'Action & Adventure', 'Animation', 'Comedy',
       'Documentary', 'Drama', 'Sci-Fi & Fantasy'))]
    genre = SingleSelectField(options=genre_options)

    def prepare(self):
        form_options = getattr(tmpl_context, 'form_options', {})
        for c in self.child.children:
            c_options = form_options.get(c.key, {})
            for name, value in c_options.items():
                setattr(c, name, value)
        super(MovieForm, self).prepare()

class RootController(BaseController):
    @expose('tw2ctest.templates.index')
    @validate(MovieForm)
    def index(self, **kw):
        tmpl_context.form_options = {'genre': {'options':['a', 'b']}}
        tmpl_context.form = MovieForm

        return dict(page='index')

But I see your point, being able to call .req and alter the widget
would have been a lot easier.

I think that this is just a matter of adding to req the check for
validated_widget as display already does, this would just mean moving
the code from display to req as display calls req itself.

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

Reply via email to