[Zope] Form Persistence Strategy

Aurélien Campéas aurelien.aa@wanadoo.fr
10 Jul 2003 02:54:08 +0200


Hello Tom, 

I am no expert on this and I am too trying to do the same thing as you
do. Here are what I believe are the right steps to begin with (this is
my ugly, quick and dirty and not yet finished nor tested howto) :

1 - build a simple product, following advice as here :
http://www.zope.org/Documentation/Books/ZDG/current/index_html (chapter
4 and 5 will mostly help you) - you should be able to quickly build up
some simple persistent python class (a structure with getter/setter
methods is the simplest thing you can do)

2 - build your form using formulator - this is the incredibly easy part
- check that your form fields match your product's fields

3 - build a validating script (a python script you may name
'check_form') like this one (taken from a zopelabs recipe) :

#This form checks request data against a formulator object.

from Products.Formulator.Form import FormValidationError

# note that 'candidat' is my product's name &&
# it's currently in the acquisition path of the script
form = context.candidat

req = context.REQUEST
if req.REQUEST_METHOD != 'POST':
 return {}

error = ''
formErrors = {}
try: 
 form.validate_all_to_request(req)
except FormValidationError, e:
 for i in e.errors:
  title = i.field.get_value('title')
  text = i.error_text
  formErrors[title] = text
 return (0, formErrors)
except:
 return "Erreur inconnue, bug ! SIGNALEZ LE !!!"

results = {}
results ['civilite'] = req.field_civilite
results ['prenom'] = req.field_prenom
#etc, here the dict keys should match YOUR field names

return (1, results)

4 - now the hardest part (for someone who barely groks zpt) : write a
template page that displays your form fields, tell the user when there
are errors, or redirects the user to some success displaying page...
something like that may help you (from the same zopelabs recipe) : 


<tal:block define="global result python:here.check_form(here.candidat)"
/>
<tal:block define="global ok python:result[0]" />
<tal:block define="global formData python:result[1]" />


<span tal:condition="python:test(ok == 0)"
      tal:define="errkeys python:formData.keys()">
Le(s) champ(s) suivant(s) sont incorrectement rempli(s) : 
<table tal:repeat="err errkeys">
   <td tal:content="err">champ erreur</td>
</table>
<br>
<p>
Veuillez retourner sur le formulaire et 
corriger ces erreurs.
</p>
<p>
Merci de votre patience.
</p>
</span>

<span tal:condition="python:test(ok == 1)">
<p>Vos coordonnées et vos fichiers ont été enregistrés et seront bientôt
revus par 
l'un de nos collaborateurs.</p>
<p>Merci de votre patience... </p>
<p>IN FACT HERE YOU SHOULD CALL SOME SCRIPT THAT DOES BUILD ONE PRUDUCT
INSTANCE </p>
<p>MAYBE :</p>
<span tal:define="final_cut python:here.build_product(formData)"/>
</span>
</span>

5 - Write the product instantiation script. takes one dictionnary as
parameter... This is on my todo list...

6 - Profit !!!


Le jeu 10/07/2003 à 01:17, Tom Affinito a écrit :
> HI Zope Experts!
> 
> Hopefully I have a simple problem.....I'm trying to figure out to display and validate a form, then store the form data into a searchable ZCatalog in the zodb.
> 
> I've looked at Formulator, but I don't know how to deal with the persistence issue (and I can make the form easily enough in HTML anyway and call a handler script, so that's fine).
> 
> I've read the Catalog chapter in the Zope Book, but making a Product doesn't seem to help me as my user's won't be in the ZMI.
> 
> I haven't found anything clear in the Admin's Guide that is directly useful for someone trying to write DTML or Page Tempates from within the Zope context.
> 
> I'd really just like to write a certain type of Python class (maybe using records?) and instantiate it and store it from some DTML so everything works....this has got to be easy, right? Yet I haven't found one clear recipe/example/HowTo.....
> 
> Tom the Zope Novice
> 
> _______________________________________________
> Zope maillist  -  Zope@zope.org
> http://mail.zope.org/mailman/listinfo/zope
> **   No cross posts or HTML encoding!  **
> (Related lists - 
>  http://mail.zope.org/mailman/listinfo/zope-announce
>  http://mail.zope.org/mailman/listinfo/zope-dev )