Re: Writing a validator to look for cookie data
Matthew Wilson <[email protected]> Thu, 12 Mar 2009 21:58:33 -0400
| Newsgroups | gmane.comp.python.formencode |
|---|---|
| Message-ID | <[email protected]> |
Thanks for the tip. Any idea why Any() doesn't seem to run in my code when no value exists? Matt On Thu, Mar 12, 2009 at 2:18 PM, Ian Bicking <[email protected]> wrote: > For something like this, I usually handle this stuff with unencoded > values. That is, I don't hit the validator at all when rendering the > form. I'd just get the defaults like: > > if req.method == 'POST': > defaults = req.POST.mixed() > force_defaults = True > else: > defaults = {} > force_defaults = False > defaults.update(cgi.parse_qs(req.cookies.get('form_defaults', ''))) > defaults.update(req.GET.mixed()) > body = htmlfill.render(form, defaults, force_defaults=force_defaults) > > If you want to use a validator, I'd use it on the line "defaults={}", > and replace that with something that grabs the encoded values from > some existing model, then runs "defaults = > schema.from_python(values)". > > On Thu, Mar 12, 2009 at 12:39 PM, Matthew Wilson <[email protected]> wrote: >> I store fields from form submissions into a cookie, and then when a >> user hits a form, I prepopulate the form with that data. >> >> I want to write a validator that first tries to get values from the >> query string, and if that is None, then the validator should check the >> cookie. >> >> Here is what I have: >> >> class LookInCookie(FancyValidator): >> """ >> Retrieves something from the cookie. Depends on a state with an >> attribute named 'simple_cookie'. >> """ >> >> def __init__(self, k, d=None): >> """ >> Return k from the cookie, or d if it isn't there. >> """ >> self.k, self.d = k, d >> >> def to_python(self, value, state=None): >> >> log.debug("Inside LookInCookie.to_python with locals: %s" >> % locals()) >> >> if hasattr(state, 'simple_cookie') \ >> and state.simple_cookie.has_key(self.k): >> >> return state.simple_cookie[self.k] >> >> else: >> return self.d >> >> Then I tried to combine this validator with Any() like this: >> >>>>> from turbogears.util import Bunch >>>>> s = Bunch(simple_cookie={'city':1}) >>>>> v = Any(LookInCookie('city'), validators.Int()) >>>>> v.to_python('99', s) >> 99 >>>>> v.to_python('abc', s) >> 1 >> >> So far, so good! First, the Int validator runs, then when Int fails, >> I get the value from the cookie s. >> >> However, here's where things fall apart. When there is no value >> passed in, nothing comes back out! >> >>>>> v.to_python('', s) >>>>> v.to_python(None, s) >> >> >> How do I force validators to run even when the value is missing? >> >> More generally, is there a better solution to what I'm trying to do? >> >> -- >> Matthew Wilson >> [email protected] >> http://tplus1.com >> >> ------------------------------------------------------------------------------ >> Apps built with the Adobe(R) Flex(R) framework and Flex Builder(TM) are >> powering Web 2.0 with engaging, cross-platform capabilities. Quickly and >> easily build your RIAs with Flex Builder, the Eclipse(TM)based development >> software that enables intelligent coding and step-through debugging. >> Download the free 60 day trial. http://p.sf.net/sfu/www-adobe-com >> _______________________________________________ >> FormEncode-discuss mailing list >> FormEncode-discuss-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org >> https://lists.sourceforge.net/lists/listinfo/formencode-discuss >> > > > > -- > Ian Bicking | http://blog.ianbicking.org > -- Matthew Wilson [email protected] http://tplus1.com ------------------------------------------------------------------------------ Apps built with the Adobe(R) Flex(R) framework and Flex Builder(TM) are powering Web 2.0 with engaging, cross-platform capabilities. Quickly and easily build your RIAs with Flex Builder, the Eclipse(TM)based development software that enables intelligent coding and step-through debugging. Download the free 60 day trial. http://p.sf.net/sfu/www-adobe-com