The ValidatorConverters are one of the really neat ideas from FunFormKit (though I use 
dalchemys FormKit)

I found myself wanting to do some additional Validation of Query Strings and Session 
values and I wanted to re-use the ValidatorConverters that I had.

The code I'm going to present isn't pretty, maybe someone has done this better already 
and if so I'd prefer to use that but its a *technique* I think has some value even if 
my coding is under-par.

Even a simple webpage might take a query-string value like 'id'. You need to do 
several tests :

1. Make sure the query string contains "id".
2. Make sure "id" is numeric and if not, try to convert it.
3. Make sure "id" is in range.
4. If any of the above fail, redirect somewhere to say "Nice try buddy"
5. Put "id" somewhere useful so you don't have to refer to it as 
self.request.field('id') - maybe self.id would be nice?

The following technique allows a page to register a set of Session and QueryString 
validators - perhaps inheriting some from its parent.

somepage.py :

from MyWeb.Code.Templates.SitePage import SitePage
from MyWeb.Code.Validators.PageStateChecks import SessionValidatorConverter, 
QueryValidatorConverter
from FormKit.Validators import MaxLength, NotEmpty, ConvertToInt

class somepage(SitePage):

    def setupValidators(self):
        SitePage.setupValidators(self)
        svc = SessionValidatorConverter
        
self.validators.append(svc(self.session(),'userid','notloggedin.html',[NotEmpty(),MaxLength(10)]))
        

        qvc = QueryValidatorConverter
        
self.validators.append(qvc(self.request(),'test','invalidparams.html',[NotEmpty(),ConvertToInt()]))
        

SitePage.py :

class SitePage(Page):
    def __init__(self):
        .....setup...
        self.validators = []
  
    def awake(self,trans):
        Page.awake(self,trans)
        self.processValidators()

    def setupValidators(self):
        """Override in children to add Query/Session validators to self.validators"""
        pass

    def processValidators(self):
        """process validators for this page, add processed items as values of the page 
or forward url to some other place (failure message page)"""
        self.setupValidators()
        for v in self.validators:
            result, value = v.validate()
            if result:
                #add to self
                self.__dict__[v.id] = value                
            else:
                self.forward(v.url)

PageStateChecks.py :

from FormKit.BaseValidatorClasses import ValidatorConverter, InvalidField

from FormKit.BaseValidatorClasses import ValidatorConverter, InvalidField

class SessionValidatorConverter:
    def __init__(self,session,id,url,validators=[]):
        self._session = session
        self.id = id
        self._validators = validators
        self.url = url
        self._error = ''

    def validate(self):
            value = self._session.value(self.id,default=None)
            result = 1
            try:
                for v in self._validators:
                    value = v._attemptConvert(value)
            except InvalidField, msg:
                self._error = str(msg)
                value = None
                result = 0
            return result, value
                

class QueryValidatorConverter:
    def __init__(self,request,id,url,validators=[]):
        self._request = request
        self.id = id
        self._validators = validators
        self.url = url
        self._error = ''

    def validate(self):
            try:
                value = self._request.field(self.id)
            except KeyError:
                value = None
            result = 1
            try:
                for v in self._validators:
                    value = v._attemptConvert(value)
            except InvalidField, msg:
                self._error = str(msg)
                result = 0
                value = None
            return result, value


-------------------------------------------------------
This sf.net email is sponsored by:ThinkGeek
Welcome to geek heaven.
http://thinkgeek.com/sf
_______________________________________________
Webware-discuss mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/webware-discuss

Reply via email to