Forums / Developer / Form validation

Form validation

Author Message

Jérôme Vieilledent

Monday 03 September 2007 7:49:28 am

Hello,

I would like to know how I can do form validation in EZ Publish (I work with 3.9.2). Indeed, this part is not well documented unfortunately :-(.
I saw that there are several ways to collect information. One of them is to set class attributes for information collection. But in my case it is not possible as the form has to be built dynamically. I would like to create an action (content/action) extension, but how do I validate the form ? If a user misses a field, I have to re-display the form and to repopulate it, with a custom error message indicating that the user omitted to fill one or several fields.

Can you please help me ?

Lazaro Ferreira

Monday 03 September 2007 9:07:48 am

Hi,

May you explain your problem further

what does mean <b>the form has to be built dynamically</b> ?

Regards

Lazaro
http://www.mzbusiness.com

Jérôme Vieilledent

Monday 03 September 2007 1:27:17 pm

Hi Lazaro and thanks for your reply :-)

OK, i'm gonna try to explain further...
My site is for a travel agency and proposes several destinations. Each destination has a commercial proposition, but the user must have the opportunity to build his own. For that, each destination has predefined fields which are defined in the admin. The administrator must have the possibility to add as many fields (or options) as he wishes.
Thus, this form has to be built dynamically, with these parameters...

I made a class named <b>"Destinations"</b> with an attribute (actually 2) for the options. The datatype I chose for this attribute is <b>"Option"</b>, so that the admin would be able to add as many as he wishes. I don't know if this is the best way for that, but it seemed quite efficient to me.
You can see an example (not made with EZ Publish) here : http://horizon-pleine-nature.com/destinations/composer/2.html

Another question related with this site. I would like to do a contact form and for that I made a class "Contact", with attributes marked as "information collector". In my template I made a HTML form with <b>/action/content</b> action and a POST method. I named the submit button <b>ActionCollectInformation</b>, but when I post the form, EZ Publish tells me that <i>the object is not available</i>... I tried many things but nothink seemed to work :-(

Thanks for your help

Bruce Morrison

Monday 03 September 2007 4:58:29 pm

Hi Jérôme

Our form bulider product may be of interest to you
http://ez.no/products/partner_products/form_builder

Cheers
Bruce

My Blog: http://www.stuffandcontent.com/
Follow me on twitter: http://twitter.com/brucemorrison
Consolidated eZ Publish Feed : http://friendfeed.com/rooms/ez-publish

Jérôme Vieilledent

Tuesday 04 September 2007 6:59:39 am

Thanks Bruce :-), but I need an open source solution since I'm learning EZ Publish... I have to understand how it works.

Olivier Ouin

Tuesday 04 September 2007 11:23:05 am

About your "validation" question, I think using the "option" datatype as information collector would be a good choice if you didn't have the text to set when selecting "other".

In EZP, validation implementation is bring by datatypes themselves.
See these methods :
validateClassAttributeHTTPInput (validate input from a class edit form)
validateObjectAttributeHTTPInput (validate input from a content object edit form)
validateCollectionAttributeHTTPInput (validate input from an information collector form)
You can find them explained by comments in eZDataType class, in the /kernel/classes/ezdatatype.php.
Each datatype extends this class, and need to overload these methods to implement their specific validation.

If you can't found existing datatypes that meet your needs, I suggest you to build your own, and made it to work as an information collector.

You can also imagine not use information collection but rather create custom objects in your content tree, but it won't spare you the task to write custom datatypes if it's really needed.. you will have to validate the object rather than the information collection : the complexity is only moved, and more, it may make your content tree becoming really huge, and the collected data would be more difficult to use (especially if usage is externalised).

The last otion I see would be to create a custom eZ module, in which you will be free about data storing, retrieving, validating and so. You can build your script nearly as you would have done with a standard php site, and associate it with a template.

For your second question about the "object is not available error", do you have passed the object IDs into hidden fields into your form ?
You should at least have something like this in your template:
<input type="hidden" value="{$object.id}" name="ContentObjectID"/>

Jérôme Vieilledent

Saturday 15 September 2007 6:02:57 am

Thanks for your reply Olivier.

Thanks to you I managed to make my contact form to work :-).
For the other validation stuff, I've decided to make a custom form action bundled in an extension (it seems to be quite easy). The problem is that this part of EZ Publish is very poorly documented... How can I validate my form properly (and so associate a template to show the errors and re-popuplate the form), and how can I just display a confirmation message ?

I read in the book that it is also possible to make a module for that, and to post the form to a module view, but it seems to be much more complicated...
The only thing I want to do is to simply validate my form...

Thanks for your help :-)

Matteo Giordani

Thursday 04 October 2007 3:14:24 am

Hello!
I have the same problem with the form validation issue.
I've developed a custom form...something like that:

http://ez.no/ezpublish/documentation/customization/components/information_collection

Everything work fine, the validation seems work... but i'm not able to retrive any validation error message.

that kind of include doesn't work...

{include name=Validation uri='design:mystandardoverride/mycollectedinfo_validation.tpl'
class='message-warning'
validation=$validation collection_attributes=$collection_attributes}

i don't know why!!!
Thanks for your help.