On Tue, 2007-23-10 at 10:15 -0500, Lukasz Szybalski wrote:
> On 10/22/07, iain duncan <[EMAIL PROTECTED]> wrote:
> >
> > On Mon, 2007-22-10 at 11:36 -0500, Lukasz Szybalski wrote:
> > > Hello,
> > > I am trying to force a user to put in a 4 character year 1999,2004 etc
> > > How do I do it in validators?
> > >
> > > This code below gives me a number and size of the field. Is there
> > > another attribute that will force it to be 4 long?
> > >
> > > YEAR=widgets.TextField(validator=validators.Number(not_empty='True'),attrs={'size':4})
> > >
> > > Lucas
> >
> > Hi Lucas, you should look in the turbogears docs and formencode docs for
> > how to make your own validator by subclassing Fancy Validator. It's not
> > too hard, and once you've done it once, it's dead easy to do it the rest
> > of time, allowing you to cusomize exactly how your constraints combine
> > and what error messages you want to produce.
> >
> > The other option is to combine the above with the regular expression
> > validator and pass in the re "\d{4}" or "\d\d\d\d". However, that won't
> > let you as easily put in reasonable limits too. With a custom one you
> > could make it accept and convert only a sensible range of years.
> >
> Actually I found an easier way.
> 
> YEAR=widgets.TextField(attrs={'size':4,'maxlength':4},validator=validators.All(validators.Number(not_empty='True'),validators.MinLength(4)))

Note however, that the above does not actually do the validation you
mentioned on the server side. It won't prevent someone posting an
overlong year to your app, you have limited the input size only, which
is easily bypassed. ( How much that matters is application specific of
course ) If you don't want the trouble of making a validator that checks
range, the formencode RE validator can enforce a max length of 4 digits
on the server side using just the RE I mentioned.

HTH
Iain



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"TurboGears" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/turbogears?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to