[mod_python] Validating input - Form Handler component

Scott Chapman scott_list at mischko.com
Thu Oct 5 16:39:30 EDT 2006


Here's a more dictionary oriented version of Julien's idea:

class FormDict(dict):
     '''This is a special dict that checks items as they are assigned.
     It would be useful for web form submissions, etc.
     At dict creation, errors are accumulated, if any, and sent back as a 
group.'''
     def __init__(self,**dictparms):
         self.__valid_keys = ('id',)
         errors = []
         for key in dictparms:
             try:
                 self.__setitem__(key, dictparms[key])
             except RuntimeError,e:
                 errors.append(str(e))
         if errors:
             raise RuntimeError(errors)
     def __setitem__(self, key, value):
         if not key in self.__valid_keys:
             raise RuntimeError, "Invalid Key: %s" % key
         if key =='id':
             try:
                 mvalue = int(value)
             except:
                 raise RuntimeError, "Invalid ID: %s" % mvalue
             if mvalue <= 0:
                 raise RuntimeError, "Invalid ID: %s" % mvalue
             super(FormDict, self).__setitem__(key, mvalue)
try:
     md = FormDict(id=1,name='bill',address='123 any street')
except RuntimeError, e:
     error_list = list(e[0])
     for error in error_list:
         print error

Julien Cigar wrote:
> Hello,
> 
> What I ususally do is a Model object with properties, something like 
> this: http://rafb.net/paste/results/LwMH9m76.html
> Then in my Controller I have a method to parse the form, something like :
> 
> def parse_form(self, themodelobject) :
>    errors = []
>    for col in themodelobject.columns:
>       try:
>          setattr(themodelobject, col, self.params.get[col])
>       except Exception, e:
>          errors.append(str(e))
>    return errors
> 
> def add(self):
>    errors = parse_form(myModel())
>    if not errors:
>        ...
>    else:
>        ...
>     
> (it's a basic and limited example, but it show you the principle)
> 
> Julien
> 
> durumdara wrote:
>> Hi !
>>
>> Do you knows about a Form handler component (module, class, unit, 
>> package, etc.) that can uniformize the form handling on the web ?
>>
>> The html form handling forces me to I repeat some of the codes in many 
>> modules.
>>
>> Example:
>> 1. Init
>> 2. Get inputs
>> 3. Validating inputs: check field names, data types, I need to open 
>> queries to see that actual data is valid (and existing), require all 
>> the fields I need to make the next step, convert data from text to any 
>> type, protect fields from SQL injection, etc.
>> 3. Make operation (insert, update, delete, or select)
>> 4. Show the results, build the result table and/or the form again.
>>
>> These steps are repeating modules by modules...
>>
>> Do you heard about something like this, or I need to write this 
>> component ?
>>
>> Thanks for your help:
>> dd
>>
>>
>>
>> _______________________________________________
>> Mod_python mailing list
>> Mod_python at modpython.org
>> http://mailman.modpython.org/mailman/listinfo/mod_python
> 
> 



More information about the Mod_python mailing list