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.
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.
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.)