Alessandro,
You're right, it works by passing directly a SQLA query object like this:

    site_id = twf.SingleSelectField(
                  validator=twfv.OneOf(model.DBSession.query(model.Group.group_id).filter(model.Group.is_real_site>0)),
                  label_text="Site",
                )

But... I'd like to decorrelate SQLA queries from my forms. What type of object is to be sent?
  • It works with a SQLA query object,
  • It works statically with a list of values,
  • It does not work with a function given as parameter,
  • It does not work more than once with an iterator,
  • ...
What type can I pass except an SQLA object ?

Thanks

Damien

Le 12/07/2011 14:34, Alessandro Molina a écrit :
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