Warning: Can't synchronize with repository "(default)" (Unsupported version control system "svn": No module named svn). Look in the Trac log for more information.

Ticket #696 (closed defect: fixed)

Opened 12 years ago

Last modified 10 years ago

NotEmpty validator doesn't work on CheckBoxList

Reported by: tlesher@… Owned by: anonymous
Priority: normal Milestone:
Component: TG Widgets Version: 0.9a1
Severity: normal Keywords:
Cc:

Description

(version 0.9a2, but that version isn't in Trac yet)

I want to validate that at least one of the checkboxes in a CheckListBox? is selected.

I have this form:

from turbogears import widgets as W
from formencode import validators as V
task_form = W.TableForm(fields=[W.CheckBoxList(name='tasks',
                                               label='Do These:',
                                               options=[('spam', 'Eat Spam'),
                                                        ('eggs', 'Eat Eggs')],
                                               validator=V.NotEmpty)],
                        submit_text='Eat')

with this template named "choosetasks.kid":

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:py="http://purl.org/kid/ns#"
    py:extends="'master.kid'">

<head>
    <meta content="text/html; charset=UTF-8" http-equiv="content-type" py:replace="''"/>
    <title>Choose Tasks</title>
</head>

 <body>
   <div id="main" py:content="form(action=action)"/>
 </body>
</html>

and with these controller methods:

   @turbogears.expose(template='.templates.choosetasks')
   def choosetasks(self):
       return dict(form=task_form, action='dotasks')

   @turbogears.expose()
   @turbogears.validate(form=task_form)
   @turbogears.error_handler(choosetasks)
   def dotasks(self, **data):
       return '\n'.join('<p>%s:%s</p>' % (k,v) for k,v in data.iteritems())

The goal is to make sure that at least one item is selected (you can eat one or both, but you have to eat something). The validator, though, doesn't seem to be doing its job.

What I see is:

Check "Eat Spam": tasks : [u'spam']

Check "Eat Eggs": tasks : [u'eggs']

Check both: tasks : [u'spam', u'eggs']

Check neither: tasks : []

I'd expect the validator to fire on the last test, but it's not happening.

Attachments

fix-for-696.patch Download (4.4 KB) - added by michele 12 years ago.
First attempt
is_named.patch Download (3.3 KB) - added by michele 12 years ago.
is_named fixes
fix-for-696-take2.patch Download (5.9 KB) - added by michele 12 years ago.
New version, way nicer
fix-for-696-take3.patch Download (6.1 KB) - added by michele 12 years ago.
NullValidator? returns None if the key is missing
easy_is_required_for_fields.patch Download (8.9 KB) - added by michele 12 years ago.
is_required made easier

Change History

Changed 12 years ago by michele

First attempt

Changed 12 years ago by michele

is_named fixes

Changed 12 years ago by michele

New version, way nicer

Changed 12 years ago by michele

NullValidator? returns None if the key is missing

comment:1 Changed 12 years ago by michele

Fixed in r1100.

I suggest you use a validator like String(not_empty=True) instead of NotEmpty?().

comment:2 Changed 12 years ago by michele

  • Status changed from new to closed
  • Resolution set to fixed

Forgot to close the ticket.

Changed 12 years ago by michele

is_required made easier

comment:3 Changed 11 years ago by L33tminion

  • Status changed from closed to reopened
  • Resolution fixed deleted

This problem does not appear to be fixed!

task_form = TableForm(fields=[CheckBoxList(name='tasks',
                                               label='Do These:',
                                               options=[('spam', 'Eat Spam'),
                                                        ('eggs', 'Eat Eggs')],
                                               validator=NotEmpty())],
                                               submit_text='Eat')

The above form still validates when no options are selected (and I have the same problem if I use String(not_empty=True) instead of NotEmpty()).

comment:4 Changed 11 years ago by alberto

  • Status changed from reopened to closed
  • Resolution set to fixed

The fix for #1033 should solve this too. Please reopen if it doesn't.

Alberto

Note: See TracTickets for help on using tickets.