[ZPT] Highlighting bad data fields in Formulator

Markus Kemmerling markus.kemmerling at mediaweb.at
Mon Sep 8 23:12:56 EDT 2003


At 11:07 08.09.2003 -0400, Tom P. Allred wrote:
>Hi,
>
>It seems like I've seen this question in the mailing list before but
>couldn't find it when I searched.  I'm currently using code from a
>Formulator example to display input errors to the user.  Here is the code
>from the python 'formaction' script.
>
># Here we'll catch errors
>try:
>   result = context.dataform.validate_all(context.REQUEST)
>except FormValidationError, errlist:
>   print "<font style='font: 17px'>I'm sorry. Some of the information you
>entered was either incorrect or incomplete. Please use the &quot;back&quot;
>button and fill it in correctly. Thank you!<br><br>"
>   for error in errlist.errors:
>     print '<b>' + error.field.get_value('title') + ': '+ error.error_text +
>'</b><br>'
>   print '</font>'
>   return printed
>
>What I'd like to do instead is re-display the input form with highlighted
>(say red) field labels and a message at the top asking the user to fix the
>highlighted fields and re-submit.  Here is the code displaying the fields,
>again taken from an example.  It is defined in a macro called by the
>folder's index_html.
>
><!-- This starts the table that displays all fields on the form --->
><table tal:define="form here/dataform" align=left>
>   <!-- Loop through the Formulator field ids to get the form fields --->
>   <tr tal:repeat="itemz here/dataform/get_field_ids">
>   <!-- Using span to define a block definition to hold a form field
>object --->
>   <span tal:define="fieldobj python:form.get_field(id=itemz)">
>    <th align="left">
>     <font size=4>
>      <font face="verdana">
>       <!-- Get/Display the title property of the current input field --->
>       <b tal:content="fieldobj/title">Field Display Label</b>
>       <b>:</b>
>      </font>
>     </font>
>    </th>
>    <td>
>     <!-- Use Formulator render command to create the field data input
>box --->
>     <input type="text" name="itemz"
>         tal:replace="structure python:fieldobj.render()">
>    </td>
>  </span>
>  </tr>
>
>It seems like the 'formaction' script will need to set some values for the
>error fields that the ZPT will use to set their label color.  Any
>suggestions on how to do this?

Maybe this solution is to complicated, but here is how I do it: I build a 
list of id's of the error fields (using error.field_id) in the formaction 
script, and also the msg to be displayed at the top (using 
error.error_text). I put these in the request and 'forward' to the form 
template: return container['dataform'].pt_render(). In the form I check, if 
the field is in the error fields list to set the style of the field label. 
(If there is no error, the formaction scripts does it's job, like inserting 
the data in a database, and forwards to another template, e.g. a success 
page. It's a little bit like a MVC approach with the formaction script 
being the controller and the page templates the view.)

Regards,
Markus Kemmerling




More information about the ZPT mailing list