Using formencode.validators to validate a field as Int and NotEmpty

31 views
Skip to first unread message

Julian Yap

unread,
Mar 13, 2009, 6:01:26 AM3/13/09
to TurboGears
Using formencode.validators, I have a field which accepts an Int. I
also want it to validate NotEmpty.

How can I add both validations? The Int validator also accepts an
empty field.

Below is what I have. I want field foo to also validate against being
NotEmpty.
----
from tw.api import WidgetsList
from tw.forms import TableForm, TextField, Spacer
from formencode.validators import Int, NotEmpty

class BazForm(TableForm):

show_errors = True

fields = [
TextField('bar', validator=NotEmpty),
TextField('foo', validator=Int(min=100, max=999))]

create_baz_form = BazForm("create_baz_form", action='savenew')

Christoph Zwerschke

unread,
Mar 13, 2009, 6:12:38 AM3/13/09
to turbo...@googlegroups.com
Julian Yap schrieb:

> Using formencode.validators, I have a field which accepts an Int. I
> also want it to validate NotEmpty.
>
> How can I add both validations? The Int validator also accepts an
> empty field.

As most validators, Int also supports a "not_empty" option. I.e. you can
use Int(min=100, max=999, not_empty=True).

-- Christoph

Marco Mariani

unread,
Mar 13, 2009, 6:13:05 AM3/13/09
to turbo...@googlegroups.com
Julian Yap wrote:
> Using formencode.validators, I have a field which accepts an Int. I
> also want it to validate NotEmpty.
>

I sugges you to Read The Formencode Manual


Two ways:

from formencode.validators import Int, NotEmpty, All


validator=Int(min=100, max=999, not_empty=True)

validator = All(Int(min=100, max=999), NotEmpty)


Julian Yap

unread,
Mar 13, 2009, 6:37:39 AM3/13/09
to TurboGears
Thanks all. That works for me. I am less confused.

Background noise: (... deletes the custom validator class...)
Reply all
Reply to author
Forward
0 new messages