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 #799 (closed defect: invalid)

Opened 13 years ago

Last modified 12 years ago

Number validator isn't called if input is empty

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

Description

I have attached a path which provides an allow_empty attribute for the Number validator. It means empty boxes won't be accepted.

-Rob

Attachments

validators.diff Download (708 bytes) - added by rob@… 13 years ago.

Change History

Changed 13 years ago by rob@…

comment:1 Changed 13 years ago by rob@…

  • Type changed from enhancement to defect
  • Summary changed from [PATCH] Add allow_empty attribute to the Number validator to Number validator isn't called if input is empty

Hey...

I attached this patch without testing it (my bad, but it was a straight CnP from the money validator), and I noticed that it didn't work. So, I went off investigating and noticed that if I put a big phat print statement as the first line of Number.to_python:

class Number(FancyValidator?):

messages = {

'empty': 'Empty values not allowed',

}

def init(self, allow_empty = True, *args, kwargs):

self.allow_empty = allow_empty

def _to_python(self, value, state):

""" parse a string and returns a float or integer """ print " I WAS CALLED "

I don't see it in my log if the input is empty (ie. it is "" or u""). Surely this can't be right?

My form is:

class BidForm?(WidgetsList?):

bid = TextField?(label="Bid", validator=Number(allow_empty=False))

The data I get through to my controller is "None" - which is not a number, by the way.

Cheers

-Rob

comment:2 Changed 13 years ago by anonymous

  • Version changed from 0.9a4 to 0.9a5

comment:3 Changed 13 years ago by michele

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

Since FE 0.5.1 (that ships with 0.9a5) not_empty works as expected hence you need to set not_empty=True nothing more.

>>> from turbogears import validators
>>> test = validators.Number()
>>> test.to_python('')
>>> test.not_empty
False
>>> test.not_empty = True
>>> test.to_python('')
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "/home/michele/Progetti/TurboGears/svn/1.0/thirdparty/formencode/formencode/api.py", line 305, in to_python
    raise Invalid(self.message('empty', state), value, state)
formencode.api.Invalid: Please enter a value
>>>
Note: See TracTickets for help on using tickets.