As widgets are singleton they are created once at the application
startup, so if you pass the result of a query to the OneOf it will
contain only the results that were on the database at startup time.

If you are passing an SQLAlchemy query to the validator you should be
able to inherit a version of the validator for which self.list is a
property that sets self._list and returns self._list.all() leaving all
the other code as it is.

On Tue, Jul 12, 2011 at 1:04 PM, Damien Accorsi <[email protected]> wrote:
> Hi all,
>
> I use TG2.1 with forms for which I enjoy validators methods. I especially
> use forms with select fields for which I use the validator OneOf(...) with a
> list of allowed values taken from my database. Unfortunately, this list is
> not static, so the classic way-of-build forms is not taking into account new
> values.
>
> Here is a sample code:
>
> class AffairAddForm(PapirusForm):
>   class fields(PapirusWidgetList):
>     site_id = twf.SingleSelectField(
>                   validator=twfv.OneOf(pla.getSiteIdList()),
>                   label_text="Site",
>                 )
>
>     affair_label = twf.TextField(
>                   validator=twfv.NotEmpty(not_empty=True),
>                   label_text="Label",
>                 )
>   submit_text = 'Save Affair'
>
> The function getSiteIdList() returns a list of ids taken from my database.
> I'd like to build this dynamically, so that if I add a new value in the
> database, then the OneOf() validator will also accept this new value.
>
> How can I do this ? I looked at the FormEncode and ToscaWidget
> documentations but didn't find what is the best way to do so.
>
> Thanks in advance.
>
> Damien
>
> --
> 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.
>

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