[ZPT] FORMs and processing

jmr@computing.com jmr@computing.com
Wed, 27 Jun 2001 23:19:40 -0500 (CDT)


tony.mcdonald> What's the received wisdom on using FORMs and
tony.mcdonald> PageTemplates? After a nightmare project where HTML
tony.mcdonald> coders were mangling DTML code I've decided to look at
tony.mcdonald> ZPT in a lot more detail...

The paradigm I use a lot goes like this:

One form (PT) and one associated python script.  The PT is the one
that gets published.  The PT either displays the form or displays the
success message.  The action on the form goes back to itself.

The PT calls the python script.  The script returns a dictionary which
includes a "success" variable, perhaps a "results" variable (if there
is data to get displayed afer successful posting, and either a simple
error variable or an error dictionary (keyed by form field name).  All
this stuff determines how the PT will render itself.

The script checks to see if the form has been submitted before (I use
the existance of a hidden variable as a flag) and if so, validates the
data.  If it hasn't been filled in, in some cases it may prefill some
fields.

If the data is not valid, the form is redisplayed with error messages
as appropriate... if it is valid, a success message is displayed
instead.  

Typically looks something like this:

This example is about creating accounts; has optional output for general
errors, errors per field, debug output.. 

<html...head...body>	  
   <div metal:fill-slot="main"
     tal:define="script_output here/accounts/adduser">
	<div tal:condition="script_output/success | nothing">
	  <div tal:replace="structure script_output/debug | nothing">
	    a debug message would go here if there was one returned...
	  </div>
	  <h3>Your account has been created.</h3>
	  <span tal:condition="not:request/no_mail | default">
	    <p>
	      A random password has been generated and was mailed to the
	      address you gave (<span tal:replace="request/mail">
		joeuser@someplace.com</span>).</p>
	    <p>After you have received your temporary password via email,
	      you may go <a href="accounts/change_password.html">here</a>
	      to change it.</p>
	  </span>
	</div>

	<form action="account_form.html" method="post"
	      tal:condition="not:script_output/success | default">
	  <input type=hidden name="kilroy"
		 tal:attributes="value request/URL0">
	  <h2 align=center>Create New Account</h2>
	  <span tal:condition="script_output/errors/general | nothing">
	    <div align=center tal:content="script/output/errors/general"></div>
	  </span>
	  <table border="0" cellpadding="3" cellspacing="5" width="720" align="center">
	    <tr>
	      <td><b>User id </b> (We suggest your Email Address)</td>
	      <td><input type="text" name="uid" size="24"
			 tal:attributes="value request/uid | nothing">
		  <span tal:condition="script_output/errors/uid | nothing"
		    tal:content="script_output/errors/uid"></span></td>
	    </tr>
	    <tr>
	      <td><b>Email Address </b> (Must be a good address)</td>
	      <td><input type="text" name="mail" size="24"
			 tal:attributes="value request/mail | nothing">
		  <span tal:condition="script_output/errors/mail | nothing"
		    tal:content="script_output/errors/mail"></span></td>
	    </tr>
	    ...


(As I go over this, I find some optimizations I could make.... but it
gets the concept across.  :)


Jim Rowan
DCSI
jmr@computing.com