On Monday 07 April 2008 18:25:27 Noufal wrote:
> On Apr 7, 2:04 am, "Diez B. Roggisch" <[EMAIL PROTECTED]> wrote:
> > Perfectly legal - and you can pass a callable to the select-field's
> > options as well, so it gets evaluated every time the form is rendered,
> > using the current DB-values.
>
> Thanks for bearing with me but I think there's some fundamental
> problem that I'm not dealing with here.
> I'll paste the relevant parts of the code here.
>
> # My list of fields is like so
> class CreateRequestFields(tg.widgets.WidgetsList):
> creator =
> TextField("creator",default=identity.current.user_name,attrs={'readonly':'t
>'},validator=vd.NotEmpty()) stream =
> MultipleSelectField("Stream",options=[(x.name,x.name) for x in
> model.Stream.query.all()],validator=vd.NotEmpty())
> change_type = SingleSelectField("change_type",label = "Nature of
> change",options = [(x.id,x.name) for x in
> model.ChangeType.query.all()],validator = vd.NotEmpty())
>
> #Inside the Root controller, I have these methods
>
> @expose(template=".templates.create")
> @identity.require(identity.not_anonymous())
> def create(self,tg_errors = None):
> if tg_errors:
> flash ("There was an error in the form")
> table = TableForm("Table form",
> fields = CreateRequestFields,
> method = "post",
> action="save_request",submit_text = "Create request")
> return dict (table = table)
>
>
> @expose(template=".templates.create")
> @validate(form=CreateRequestFields)
> @error_handler(create)
> @identity.require(identity.not_anonymous())
> def save_request(self,**lst):
> # This is just a dummy to see if it works.
> return dict(table = TextArea("das",default="Successful save"))
>
>
> The error I get is like this
> 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.
>
> Of course, this should be called only from within the controller, So I
> put in some try/excepts around the definition inside the
> CreateRequestFields class to work around the problem. Then I get
> errors like this
> AttributeError: type object 'CreateRequestFields' has no attribute
> 'is_named'
> I assumed that since I can "call" CreateRequestFields, I could pass it
> to the as the fields parameter and it would get called. Isn't that the
> case?
>
> I got a working solution by creating a function which constructs the
> entire form (fields and the TableForm) and passing the function as the
> argument to the validator and calling it to create the widget
> necessary to render the page. This works fine but the problem is that
> when there is an error, I don't automagically get the help messages.
> If that'd be too complicated, I can live without that but I'd still
> like to know what's wrong here and what's the right way to do it.
Try passing a callable to default, like this
TextField(default=lambda: identity.current.user_name)
I think that should work.
Additionally (or if against my belief it doesn't work) you could simply pass
the identity-stuff as *value*, as i said before, in the display-method-call
of the form. Like this:
${form.display(value={"widget_name" : identity.current.user_name})}
Should work also, as values from the request should take precedence over
passed ones (in case you re-display in case of validation-errors)
Diez
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---