On Jun 20, 2006, at 8:57 AM, samuraisam wrote:

>
> I have a form:
>
> class X(widgets.WidgetsList):
>     blog = widgets.SingleSelectField(label="Blog", options=get_blogs)
>
> with a function:
>
> def get_blogs():
>     return [(blog.id, blog.title)
>                 for blog in identity.current.identity().user.blogs]
>
> and as seen in this (unresolved) thread:
> http://groups.google.com/group/turbogears/browse_thread/thread/ 
> b1c764b56c1153eb/477c254accec0402?
> it is impossible (or at least to the best of my knowledge) to access
> identity when a form is created because, again, to the best of my
> knowledge and from the errors TurboGears is giving me, that happens
> outside of a request. Specifically:
>
> turbogears.identity.exceptions.RequestRequiredException: An attempt  
> was
> made to use a facility of the TurboGears Identity Management framework
> that relies on an HTTP request outside of a request.
>
> What I need is a way to make it so I can dynamically add options to
> X.blog from /within/ a request. I imagine I can do this by
> manipulationg the TableForm object it's placed into, but even that is
> created /outside/ of a request. So, still, I need to create options my
> SingleSelectField on the fly from wthin the action. How, though? I  
> have
> gotten this far:
>
> def action(self, tg_errors=None, *args, **kw):
>     my_form.fields.append(my_dymanically_generated_SingleSelectField)
>
> But then, why even use the forms api? I have to roll my own validation
> for that and other fields that are generated depending on the user
> accessing the action. If someone could please look at my problem, I'd
> be very greatful.

Hmmm, my guess is that "get_blogs" is being called outside the  
request when the SingleSelectField is trying to guess a validator.  
Can you please confirm this guess by providing a validator to the  
widget?

from turbogears.validators import Int
class X(widgets.WidgetsList):
     blog = widgets.SingleSelectField(label="Blog",  
options=get_blogs, validator=Int())

Should be enough.

If this works I'm not sure if it's a widgets' bug but a corner case  
involving callables which need a request availble *for* the  
SingleSelectField (which is the only one which tries to guess a  
validator).

Maybe remove this "guessing" behaviour? It clearly contradict the "in  
the case of ambiguity avoid the temptation to guess" principle IMO.

Alberto

--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to