questions about widgets and validators

1 view
Skip to first unread message

Javier Rojas

unread,
Jul 16, 2006, 10:59:22 PM7/16/06
to TurboGears
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?

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

Jorge Godoy

unread,
Jul 16, 2006, 11:08:00 PM7/16/06
to turbo...@googlegroups.com
Javier Rojas <jero...@gmail.com> writes:

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

Javier Rojas

unread,
Jul 17, 2006, 12:44:44 AM7/17/06
to turbo...@googlegroups.com
El Domingo 16 Julio 2006 22:08, Jorge Godoy escribió:
>Javier Rojas <jero...@gmail.com> writes:
>> 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.
No, it didn't work. I used the not_empty=True with a validator.Int, but
nothing happened.

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.

Alberto Valverde

unread,
Jul 17, 2006, 6:15:33 AM7/17/06
to turbo...@googlegroups.com

On Jul 17, 2006, at 4:59 AM, Javier Rojas wrote:

> 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

jerojasro

unread,
Jul 17, 2006, 4:09:40 PM7/17/06
to TurboGears

Alberto Valverde wrote:
> You should be abe to do something like:
>
> w = MultipleSelectField("mselect", validator=Int(not_empty=True))
> ...

> 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))
>
nope, It doesn't work. My code:

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

Jorge Godoy

unread,
Jul 17, 2006, 4:24:47 PM7/17/06
to turbo...@googlegroups.com
"jerojasro" <jero...@gmail.com> writes:

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

Javier Rojas

unread,
Jul 17, 2006, 4:32:55 PM7/17/06
to turbo...@googlegroups.com

I know, just exhausting  the possibilities :)

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

--
Jorge Godoy      <jgo...@gmail.com >

Jorge Godoy

unread,
Jul 17, 2006, 4:45:07 PM7/17/06
to turbo...@googlegroups.com
"Javier Rojas" <jero...@gmail.com> writes:

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

Javier Rojas

unread,
Jul 17, 2006, 9:15:15 PM7/17/06
to turbo...@googlegroups.com
On 7/17/06, Alberto Valverde <alb...@toscat.net> wrote:

> 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... ;)

I hope the reason is the latter. I tried creating a validator (subclassed from Schema) and passing it to the compound widget, but nothing,.. well, here is my code:
first, the widgets:
%%%%%%%%%%%% %< %%%%%%%%%%%%%
 class SinglePrettySelectField(wdg.SingleSelectField):
  template = """
  <select xmlns:py="http://purl.org/kid/ns#"
    size="${size}"
    name="${name}"
    class="${field_class}"
    id="${field_id}"
    py:attrs="attrs"
    >
    <optgroup py:for="group, options in grouped_options"
      label="${group}"
      py:strip="not group"
      >
      <option py:for="value, desc, attrs in options"
      value="${value}"
      py:attrs="attrs"
      py:content="desc"
      />
    </optgroup>
  </select>
  """
  params=["size"]

class SingleSelectWithControls(wdg.CompoundFormField):
  template="""
  <table xmlns:py="http://purl.org/kid/ns#" border="0" cellspacing="0"><tr><td>
  ${display_field_for(select_widget)}</td>
  <td>
  <table border="0" cellspacing="0">
  <tr><td>${display_field_for(button_add)}</td></tr>
  <tr><td>${display_field_for(button_delete)}</td></tr>
  </table>
  </td></tr>
  <tr>
  </tr>
  </table>
  """
  member_widgets=['select_widget','button_add','button_delete']
%%%%%%%%%%%% %< %%%%%%%%%%%%%

and now the controller:
%%%%%%%%%%%% %< %%%%%%%%%%%%%
f=w.TableForm(fields=[
    lw.SingleSelectWithControls(label="Algo",
      select_widget=lw.SinglePrettySelectField(name="opt",label="Options",size=5,options=[(i,str(i)) for i in range(5) ],validator=v.Int(not_empty=True)),
      button_add=w.Button(default="Add...",name="add"),
      button_delete=w.Button(default="Delete...",name="delete")
      ),
    lw.SinglePrettySelectField(name="opt2",label="Options",size=5,options=[(i,str(i)) for i in range(5) ],validator=v.Int(not_empty=True))

    ],
    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
%%%%%%%%%%%% %< %%%%%%%%%%%%%


Alberto Valverde

unread,
Jul 18, 2006, 7:52:36 PM7/18/06
to turbo...@googlegroups.com

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

Reply all
Reply to author
Forward
0 new messages