On Oct 27, 2006, at 6:06 AM, Anthony Baxter wrote:
>
> Say I define something like this at the top level of controllers.py:
>
> class CommentFields(widgets.WidgetsList):
> name = widgets.TextField(validator=NotEmpty())
> url = widgets.TextField(validator=NotEmpty())
> post_id = widgets.HiddenField()
> email = widgets.TextField(validator=Email(not_empty=True),
> attrs={'size':30})
> text = widgets.TextArea(validator=NotEmpty())
> remember = widgets.CheckBox(label="Remember me")
> comment_form = widgets.TableForm(fields=CommentFields(),
> action="comment")
>
> Now, in a controller method, I want to get hold of the post_id field
> and set the value of the hidden form field to the current post_id
> (which
> is only known at the time the controller method runs). At the moment,
> I'm doing this by defining the CommentFields class inline in the
> controller
> method, and setting the value at the time the class is defined, but
> a) this seems inelegant
> b) it means the CommentFields class isn't available to use as a
> validator
> in the controller's comment() method (the target of the form).
>
> I could always dig through the class object and set the post_id,
> but that
> strikes me as cruising for a bruising, since it's a single class
> object.
And you shouldn't. Setting class attributes on widgets after they are
initialized is quite likely to give you headaches later with thread-
safety issues.
You can pass a value to any inner field of a compound widget in a
dictionary when displaying it:
comment_form.display(value={'post_id':4})
HTH,
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
-~----------~----~----~----~------~----~------~--~---