I have a multipleSelectField, and when I use a validator with it, the
validation and conversion is made over every selected element (e.g, with
validators.Int); if I select no items from it, the validator doesn't get
called. I need the validator to check if the list (the selected elements) is
empty. What validator should I use?
other question:
how can I use validators with compound widgets? I've defined validators for
all the elements within the compound widget, but they don't seem to work.
--
Javier Rojas
GPG key:
http://pgp.mit.edu:11371/pks/lookup?op=get&search=0xA1C57061
> I have a multipleSelectField, and when I use a validator with it, the
> validation and conversion is made over every selected element (e.g, with
> validators.Int); if I select no items from it, the validator doesn't get
> called. I need the validator to check if the list (the selected elements) is
> empty. What validator should I use?
The same. There are several possibilities on what you can do with an empty
input. Take a look at the docs for the validators and you'll see, for
examplo, if_empty, not_empty et al.
> how can I use validators with compound widgets? I've defined validators for
> all the elements within the compound widget, but they don't seem to work.
Take a look at the Select Shuttle I posted in another message.
--
Jorge Godoy <jgo...@gmail.com>
I wrote a validator that always raises an Invalid exception when called, and
used it with the MultipleSelectField. It is only called when there is some
element selected, when none is selected, nothing happens.
>> how can I use validators with compound widgets? I've defined validators
>> for all the elements within the compound widget, but they don't seem to
>> work.
>Take a look at the Select Shuttle I posted in another message.
Yes, you use compound widgets, but that doesn't answer my question. What I
need (and I didn't find it on your code -since is the definition of the
widget-), is to know which validators can I use with the compoundWidget.
Following the example of the SelectShuttle widget, I would like to see an
example, sort of a line like this
mywidget=SelectShuttle(validator=validators.????????)
or anything that allows to the validators within the compound widget to show
its error messages
again, all the widgets within the compound widget have a validator, and when
some of them should raise an Invalid exception (and show the error message,
and all that), nothing appears.
> Hi
>
> I have a multipleSelectField, and when I use a validator with it, the
> validation and conversion is made over every selected element (e.g,
> with
> validators.Int); if I select no items from it, the validator
> doesn't get
> called. I need the validator to check if the list (the selected
> elements) is
> empty. What validator should I use?
You should be abe to do something like:
w = MultipleSelectField("mselect", validator=Int(not_empty=True))
This should make sure at least one option is selected. IIRC browsers
don't even send a variable name for a multiple select if no options
are chosen. You can provide a failsafe value fo this scenario by
passing it to the validator as the 'if_empty' parameter:
w = MultipleSelectField("mselect", validator=Int(if_empty=2))
> other question:
> how can I use validators with compound widgets? I've defined
> validators for
> all the elements within the compound widget, but they don't seem to
> work.
Validators for compound widgets should subclass
turbogears.validators.Schema. If you don't define one for the
compound widget itself then a schema will be automatically generated
including every validator present in the child widgets. If this is
not working for you, can you please submit some code that presents
this behavior? Maybe you've found a bug or maybe you're just doing
something wrong... ;)
As Jorge pointed out, SelectShuttle is a good example of a
CompoundFormField where you can look for insipiration
Alberto
f=w.TableForm(fields=[
w.MultipleSelectField(name="opt",label="Options",size=5,options=[(i,str(i))
for i in range(5) ],validator=v.Int(not_empty=True,if_empty=-45))
],
submit_text=u"Push Me!!")
class Root(tg.controllers.RootController):
@tg.expose(template="forms.templates.forms")
def index(self):
global f
return dict(form=f,title=u"Hello!!!",action="put")
@tg.expose(format="json")
@tg.validate(form=f)
@tg.error_handler(index)
def put(self,**kwargs):
return kwargs
If I don't select anything, I get an empty list. If I select something,
I get a list of integers. I've tried too using only if_empty, and too
using only not_empty, with the same results.
> Validators for compound widgets should subclass
> turbogears.validators.Schema. If you don't define one for the
> compound widget itself then a schema will be automatically generated
> including every validator present in the child widgets. If this is
> not working for you, can you please submit some code that presents
> this behavior? Maybe you've found a bug or maybe you're just doing
> something wrong... ;)
when I can get the multiple select working I'll be checking this. But
now is much more clear
> Alberto
Javier
> w.MultipleSelectField(name="opt",label="Options",size=5,options=[(i,str(i))
> for i in range(5) ],validator=v.Int(not_empty=True,if_empty=-45))
You can't say "not_empty" and "if_empty" at the same time. There's no sense
in that. From the above I presume you'd like to have just "if_empty", so that
this becomes a default value.
--
Jorge Godoy <jgo...@gmail.com>
--
Jorge Godoy <jgo...@gmail.com >
> but anyway, I tried using not_empty, and if_empty (exclusivelly), and It
> didn't worked. When I used if_empty, I didn't got the default value, but the
> empty list instead
Post a complete test application and I can take a look at it. I can
quickstart and write one, but I can take a look at one with the problem within
it.
Send it in private, please.
--
Jorge Godoy <jgo...@gmail.com>
> other question:
> how can I use validators with compound widgets? I've defined
> validators for
> all the elements within the compound widget, but they don't seem to
> work.
Validators for compound widgets should subclass
turbogears.validators.Schema. If you don't define one for the
compound widget itself then a schema will be automatically generated
including every validator present in the child widgets. If this is
not working for you, can you please submit some code that presents
this behavior? Maybe you've found a bug or maybe you're just doing
something wrong... ;)
FYI, I've comitted at 1651 in the 1.0 branch a fix for this. More
info at http://trac.turbogears.org/turbogears/ticket/1033/ and at the
attached app.
HTH,
Alberto