From: Erik F. <for...@ly...> - 2002-06-17 20:16:07
|
From: Erik Forsberg <for...@ly...> Subject: [Webware-discuss] FunFormKit and several forms on one page. To: web...@li... Date: 16 Jun 2002 22:36:21 +0200 Hi! (Tried this on the webware-discuss-list. Didn't find this list at first) I'm trying to put several forms on one page, each with it's own submit button. I run into an Exception when using a validator: FormError: Upon error you must render the form that caused the error -- form addistrictform had an error and you tried to render addareaform Here's my servlet: --snip-- from Lib.SecurePage import SecurePage from Cheetah.Template import Template from FunFormKit.Form import FormDefinition, FormServlet from FunFormKit import Field from FunFormKit.Validator import FormValidator, ValidatorConverter, \ InvalidField from Lib.Validators import ServletValidator class DistrictValidator(ServletValidator): def convert(self, districtid): try: num_districtid = int(districtid) except ValueError: raise InvalidField, self.servlet.setting('MustBeNumberMessage') district = self.servlet.getDistrict(num_districtid) if district is None: return num_districtid else: raise InvalidField, \ self.servlet.setting('DistrictNumberTakenMessage') class SystemConfig(SecurePage, FormServlet): def __init__(self): SecurePage.__init__(self) areaForm = FormDefinition('SystemConfig', 'writeForm', [Field.TextField('areaname', maxLength=255, size=25), Field.TextField('lettercode', maxLength=2, size=2), Field.SubmitButton('submit', methodToInvoke='addarea')], name="addareaform") districtForm = FormDefinition('SystemConfig', 'writeForm', [Field.TextField('districtname', maxLength=255, size=25), Field.TextField('shortname', maxLength=255, size=5), Field.TextField('districtid', maxLength=2, size=2, validators=\ [DistrictValidator(self)]), Field.SubmitButton('submit', methodToInvoke=\ 'addistrict')], name="addistrictform") FormServlet.__init__(self, 'writeContent', [areaForm, districtForm], defaultFormDefinition=areaForm) def addarea(self, fieldDict): self.addArea(fieldDict["lettercode"], fieldDict["areaname"]) self.response().sendRedirect("SystemConfig") def addistrict(self, fieldDict): self.addDistrict(fieldDict["districtid"], fieldDict["shortname"], fieldDict["districtname"]) self.response().sendRedirect("SystemConfig") def awake(self, trans): SecurePage.awake(self, trans) if trans.request().hasField("deletearea"): self.deleteArea(int(trans.request().field("deletearea"))) self.response().sendRedirect("SystemConfig") if trans.request().hasField("deletedistrict"): self.deleteDistrict(int(trans.request().field("deletedistrict"))) self.response().sendRedirect("SystemConfig") def title(self): return 'Konfiguration av FUFReg' def defaultConfig(self): secConf = SecurePage.defaultConfig(self) secConf.update({'RequiredLevel':400}) return secConf def writeForm(self): areas = self.getAreas() districts = self.getDistricts() areaRF = self.renderableForm('addareaform') districtRF = self.renderableForm('addistrictform') t = Template(file=self.getTemplateName("SystemConfig"), searchList = [{'areas':areas, 'areaRF':areaRF, 'districts':districts, 'districtRF':districtRF}]) self.write(str(t)) --snap-- (The ServletValidator class looks like this: class ServletValidator(ValidatorConverter): def __init__(self, servlet): ValidatorConverter.__init__(self) self.servlet = servlet SecurePage inherits from SitePage that inherits from SidebarPage and MiscUtils.Configurable - If you need more code for debugging, just tell me) The template looks like this: --snip-- ## mode: html <H1>Landsting och deras kommuner</H1> Klicka på Landstinget namn för att se och editera dess kommuner. <table> <tr> <th align="left">Länsbokstav</th><th align="left">Landsting</th> </tr> #for $row in $areas <tr> <td align="left">${row.lettercode}</td> <td align="left"><a href="SubAreaConfig?area=$row.id">${row.name}</a></td> <td align="left"> #if 0 == $row.noofsubareas and 0 == $row.noofsubsocieties <a href="SystemConfig?deletearea=$row.id">Radera</a> #else <font color="grey">Kan ej raderas</a> #end if </tr> #end for </table> Om ett landsting ej kan raderas beror det på att det finns föreningar eller kommuner som beror på detta landsting. <H3>Lägg till landsting</H3> ${areaRF.start} <TABLE> <tr> <td align="right" valign="top" class="label"> <b>Bokstavskod:</b></td> <td>${areaRF.lettercode} ${areaRF.lettercode.error}</td> </tr><TR> <td align="right" valign="top" class="label"> <b>Namn:</b></td> <td>${areaRF.areaname} ${areaRF.areaname.error}</td> </tr><tr> <td> </td> <td align="left" valign="top"> <input type="hidden" name="submit" value="submit"> <input type="submit" name="submit" value="Lägg till"> </td> </tr> </TABLE> ${areaRF.end} <H1>Distrikt</H1> <table> <tr> <th align="left">Nummer</th> <th align="left">Förkortning</th> <th align="left">Namn</th> </tr> #for $row in $districts <tr> <td align="left">${row.id}</td> <td align="left">${row.shortname}</td> <td align="left">${row.name}</td> <td align="left"> #if 0 == $row.noofmembers and 0 == $row.noofsubsocieties <a href="SystemConfig?deletedistrict=$row.id">Radera</a> #else <font color="grey">Kan ej raderas</a> #end if </tr> #end for </table> <P> Om ett distrikt ej kan raderas beror det på att det finns föreningar eller distriktsmedlemmar i distriktet. <H3>Lägg till distrikt</H3> ${districtRF.start} <TABLE> <tr> <td align="right" valign="top" class="label"> <b>Nummer:</b></td> <td>${districtRF.districtid} ${districtRF.districtid.error}</td> </tr><TR> <td align="right" valign="top" class="label"> <b>Förkortning:</b></td> <td>${districtRF.shortname} ${districtRF.shortname.error}</td> </tr> <TR> <td align="right" valign="top" class="label"> <b>Namn:</b></td> <td>${districtRF.districtname} ${districtRF.districtname.error}</td> </tr> <tr> <td> </td> <td align="left" valign="top"> <input type="hidden" name="submit" value="submit"> <input type="submit" name="submit" value="Lägg till"> </td> </tr> </TABLE> ${districtRF.end} --snap-- (Might be less understandable if you don't know swedish :) ) The 'defaultFormDefinition' argument is confusing me - perhaps FunFormKit can only do one form per page at a time? Or am I doing something else wrong? The forms both render nicely and work as they should, except for errorhandling. Regards, \EF -- Erik Forsberg http://www.lysator.liu.se/~forsberg/ GPG/PGP Key: 1024D/0BAC89D9 <for...@ly...> Key Fingerprint: B308 87FC 566E 825A 5ABC 247C AC9B AB14 0BAC 89D9 |