[Zope] Very lightweight form validation

Jim Penny jpenny at universal-fasteners.com
Thu Jun 24 11:21:12 EDT 2004


On Mon, 21 Jun 2004 17:02:15 -0500
Kirk Strauser <kirk at daycos.com> wrote:

> I need to do some minimal validation on a few forms.  The built-in 
> typechecking (http://www.zope.org/Members/Zen/howto/FormVariableTypes)
> does about 99% of what I need, with the exception of outputting useful
> error messages.
> 
> I've looked at some of the various form-processing add-ons but all of
> them see very "heavy" when what I mainly want to do is ensure that the
> user remembered to fill in their last name, and if not, tell them that
> they need to enter it and try again.  Seriously, that's about the
> sophistication level I'm targeting.
> 
> Any suggestions?

OK -- this may not be popular -- throw away the builtin typechecking. 
Do it yourself, it is not hard, at all.

index_html, for me, is always a script python.  It might look like:

request=context.REQUEST 
if not request.has_key('next_state')
  return main_menu_pt(context, request)
ns=request['next_state']
if ns=='Enter Name':
  return name_form_pt(context, request)
elif ns=='Save Name':
  err=container.check_name_form_ps(context, request)
  if err:
    return name_form_pt(context, request, error_message=err)
  # save the name, my example is sql
  container.save_name_sql(request)
  return main_menu_pt(context, request)

Now,  check_name_from_ps looks like:

request=context.REQUEST
err=''
nm=request['name'].strip()
request.set('name', nm)
if not nm:
  err+='<p>You must enter a name.</p>'
# name can't be entered twice
res=container.select_name_sql(name=nm)
if len(res)>0:
  err+='<p>The same name cannot be entered twice.</p>'
...
return err

Finally, name_form_pt looks something like

<html>
...
  <div tal:content="options/error_message|nothing"></div>
  
  <form method="post" action="container/absolute_url">
    Name:  
    <input type="text" size="25" 
         tal:attributes="value request/name|nothing">
   ...
     <input type="submit" name="next_state" value="Save Name">
  </form>
...
</html>

Very flexible, pretty simple, and index_html is strictly boilerplate. 
Either present a form; or check a form, if error found, present the
original form with errors noted and most recent content, or if no error
found, do the side-effects and present the next suitable from.

Jim Penny

> -- 
> Kirk Strauser
> The Day Companies
> 
> _______________________________________________
> Zope maillist  -  Zope at zope.org
> http://mail.zope.org/mailman-20/listinfo/zope
> **   No cross posts or HTML encoding!  **
> (Related lists - 
>  http://mail.zope.org/mailman-20/listinfo/zope-announce
>  http://mail.zope.org/mailman-20/listinfo/zope-dev )
> 
> 


More information about the Zope mailing list