[ZPT] DRAMA: Can Formulator Play Nicely With ZPT? (Part II)

Jeffrey P Shell jeffrey@cuemedia.com
Sun, 13 Oct 2002 10:13:50 -0600


>
>>> My problems are these:
>>> Why doesn't the form validate? How do I make it validate?
>>
>> If Formulator has the right information (depending on how you either 
>> populate your input tags, or generate them - which is why I use 
>> 'tal:replace="structure ..."' - it helps ensure that Formulators 
>> validators get what they want), you can have a Python Script like the 
>> following:
>>
>> from Products.Formulator.Errors import FormValidationError
>> request = container.REQUEST
>> errors = []
>> try: context.myFormulatorForm.validate_all_to_request(request)
>> except FormValidationError, e:
>>     errors = e.errors
>>
>> ## You can go through the errors list and build a structure
>> ## like a dictionary to make hilighting error fields easier,
>> ## or you can just loop through the errors in the return
>> ## page to display error fields and the validation messages
>> ## in a single block.
>> edict = {}
>> for error in errors:
>>     edict[error.field_id] = error.error_text
>>
>> if errors:
>>     return context.restrictedTraverse('mytemplate.pt')(
>>         request, errors=errors, edict=edict,
>>         )
>> else:
>>     ## do success handler
>
> Nice script. However, I recall that Formulator has it's own built-in 
> checking, for things like email syntax, etc., and none of this seems 
> to work properly. Any suggestions? Also, I presume we *still* have to 
> send to a DTML script in order to invoke <dtml-sendmail>. Am I 
> correct, or do you have a ZPT work-around for this?

Did you add an Email field to the Formulator form?  Those have worked 
fine for me in the past, and the validate_all() and 
validate_all_to_request() methods should catch invalid addresses (for 
however exhaustive the Email Field's check is).

For sending mail, a best practice I've found for this is to have a 
separate DTML method for doing mail, and call it from the Python 
Script.  You can just call it (like 
'context.sendRegistrationMessage(...)') before returning / redirecting 
to the next Page Template page when the form handling succeeds.