Zend_Form with XML Tutorial

03.9.2008 | 1:50 pm | Code, Zend

Since Zend_Form is released a lot of people have been searching for a good tutorial how to combine Zend_Form with XML. Rob Allen wrote a very good tutorial to use Zend_Form with some code. During my interneship i’ve been busy with Zend_Form and XML and now I like to share it with you. So here is a very easy, complete example that shows it in action. Let’s make a small login form from XML with Zend_Form.

zend_form_01


You can construct a Zend_Form directly from a configuration file written in XML or you can build it in code (like Rob Allen did in his tutorial). This example builds it from a XML file.

Let’s have a look at the inside: XML file
As you can see, I’ve added three elements (username, password and submit). We added also some options like stringLength, required, regex to make the example a bit more sofisticated. Looks quiet easy isn’t it?

Let’s generate the form:
$form = new Zend_Config_Xml('login.xml');
$this->view->form = new Zend_Form($form->user->login);

And here we are… The form is generated. Another great possibility with Zend_Form is the validation.
zend_form_02
Without any lines of code we have a very expanded validation. Yes there are some problems with the error messages at the password field… (normally the password shouldn’t be shown.) but this could be fixed with some extended Validator.

So there you have it. A very simple example of how to use Zend_Form with XML. Here’s a zip file of this project: Zend_Form_XML.zip (The package is about 3MB big. It includes the Zend Framework 1.5.0PR.)

21 Comments »

  1. Pieter’s Blog: Zend_Form with XML Tutorial…

    Pieter has blogged today with an example of using an XML ……

    Trackback by PHPDeveloper.org | 03.10.2008 | 2:17 pm

  2. [...] has blogged today with an example of using an XML configuration file together with the Zend_Form component of the Zend Framework to [...]

    Pingback by Pieter’s Blog: Zend_Form with XML Tutorial | Development Blog With Code Updates : Developercast.com | 03.10.2008 | 6:10 pm

  3. [...] has blogged today with an example of using an XML configuration file together with the Zend_Form component of the Zend Framework to [...]

    Pingback by Pieter’s Blog: Zend_Form with XML Tutorial | PHP Coding Practices - Become an expert PHP Programmer | 03.10.2008 | 7:00 pm

  4. The password is no longer echoed as part of the validation error messages or input field on submit in the current trunk (fixed for RC1, actually). ;-)

    Comment by Matthew Weier O'Phinney | 03.10.2008 | 8:21 pm

  5. Nice to see that there is progress. Zend FTW!

    Comment by Snakehit | 03.10.2008 | 8:23 pm

  6. [...] has blogged today with an example of using an XML configuration file together with the Zend_Form component of the Zend Framework to [...]

    Pingback by Pieter’s Blog: Zend_Form with XML Tutorial | Cole Design Studios | 03.10.2008 | 10:08 pm

  7. [...] Blog oraz wpis o którym mowa: http://blog.snakehit.be/2008/03/09/zend_form-with-xml-tutorial [...]

    Pingback by Śmieszni ludzie | 03.10.2008 | 11:21 pm

  8. Same thing in Jelix since several months. But our xml format is less verbose : http://jelix.org/articles/en/tutorials/simple-jforms-example

    Comment by Laurentj | 03.11.2008 | 11:24 am

  9. Do you have any idea how to configure a group of radio elements? I can’t find anything about that in the manual at framework.zend.com (or anywhere else on the net)…

    Comment by schmkr | 06.4.2008 | 10:52 am

  10. Sure, just create your element

    <language> <type>radio</type>
    <options>
    <id>language</id> <label>Language:</label>
    <multioptions> <nl>Dutch</nl> <en>English</en> <fr>French</fr>
    <de>Deutsch</de> </multioptions> </options>
    </language>

    Comment by Snakehit | 06.6.2008 | 11:33 pm

  11. Thanks!

    That worked!

    Comment by schmkr | 06.7.2008 | 12:38 pm

  12. I noticed that you have defined the local variable $formData in the if conditional block, yet referenced it in the corresponding else block; where it is out of scope.

    I assume you meant to assign this variable outside of the if statement or use a different variable in the else block.

    Thanks,
    Kieran.

    Comment by Kieran Hall | 07.2.2008 | 4:04 pm

  13. http://search.live.com/results.aspx?q=zend+tutorial&go=&form=QBRE

    first hit, not bad :)

    Just started with Zend but there is a big lack on great up2date beginner tutorials :(

    Comment by NiFkE | 07.28.2008 | 1:42 pm

  14. Thank you for sharing. I came to this site to read how things really are

    Comment by Almada | 02.6.2009 | 2:05 pm

  15. Nice work! I’ll have to do a cross post on this one ;)

    Comment by Salubr | 02.7.2009 | 7:40 am

  16. Hi,

    Thank you very much. This example is very nice and cool. I need some more help from you. Its not validating the form.

    Can you guide me in this aspect?

    Thanks.

    Comment by PixelMaker | 05.20.2009 | 8:39 pm

  17. Hello PixelMaker,

    If you can give me some more details I could help you yes.

    Comment by Snakehit | 06.12.2009 | 11:12 pm

  18. [...] Zend_Form with XML Tutorial [...]

    Pingback by 網站製作學習誌 » [Web] 連結分享 | 11.25.2009 | 3:40 am

  19. Hello,

    I am trying as someone else was earlier to use different form elements. How do I create a select element?

    Ben

    Comment by ben | 05.20.2010 | 7:21 pm

  20. Like this?

    <language> <type>select</type>
    <options>
    <id>language</id><label>Language:</label>
    <multioptions><nl>Dutch</nl><en>English</en><fr>French</fr>
    <de>Deutsch</de></multioptions></options>
    </language>

    Comment by Snakehit | 05.23.2010 | 8:31 pm

  21. This is really great : amazingly simple.

    However, is there a way to export a Zend_Form object to its XML representation ? That will be even cooler; cause i’m currently forced to export the XML manually which is not quick.

    Comment by Neamar | 06.21.2010 | 11:11 am

 

Leave a comment