I’ve been having a love/hate relationship with the Zend Framework as I attempt to learn how to use it. No doubt it is incredibly powerful but it may be overly complicated for my tastes. Maybe I was having a bad day or following bad instructions, but setting up an initial site using Zend for the first time took me HOURS! This is not the first PHP framework I’ve ever used either. I used to manage my personal site in Codeigniter and many years ago tried and loved Cake PHP. Anyway I’m not sure what the problem was, but by the time I got to the blue welcome screen I was exhausted and went no further.
More recently however, I had a need to develop an application on a server I knew had Zend installed on it. The thought of repeating the frustrations from several months earlier did not sound appealing so I have set out to experiment with using the Zend Framework without the Framework, or, in other words, the Zend classes without the MVC. Most of what I have learned so far has come from other posts on the Internet, so I now know I’m not the only one who’s ever attempted to do this.
Actually this turned out to be pretty easy. The rest of this post covers the creation of a very basic, standalone zend form.
Even though MVC is not being used, a controller of sorts is still needed to get things started. Loading Zend classes can be done by requiring the Zend Loader and then calling the loadClass static method with the name of the class needed. In this basic controller, I load my custom form class RequestForm and also Zend_View which is necessary in order to create a view object to render the form.
Forms in Zend are objects so it is necessary to instanciate an instance of a form in order to display and use it. The form is displayed on a webpage by calling the form’s render method and passing it the view object. The rest of the controller code checks whether or not the form has been posted, and if it has attempts to validate it using a very cool isValid method on the form object. Once the form is posted with valid input, that information can be accessed by calling the getValues method. This makes for a nice, lean controller.
I mentioned that forms in Zend are objects. I now love the idea of using form objects for creating forms, but this took some time to get used to. The bulk of the work here takes place in the init method. To begin, use a series of setter methods to set the various form attributes such as name, action URL and method. It’s also possible to use the setAttrib method to set custom attributes, such as “id”. There is also a setDescription method although I haven’t quite figured out what this is good for.
Creating form fields is possible by calling the createElement method with 2 arguments: 1st the type of field (ie. text, submit, etc) you need to create and 2nd the name of the field. Calling setOptions on the element it’s possible to do things such as assign a label, make the field required and filter and validate the incoming data. The ability to use Zend’s built-in form handling to filter and validate input is exactly why I wanted to use it for this project in the first place and this seems to be one of its strengths.
Last but not least, calling the addElement method actually adds the created fields to the form.
The above gets the job done, but what I’m not too happy with is the output. The result is a form structured with a definition list. Lately wrapping fields in <p> tags has been my preference but I know others like to use lists. An unordered list of errors is displayed if the field did not pass validation and the default messages are not the most user-friendly.
I hope to spend more time learning about the various ways of creating different types of elements, filtering and validating input and customizing the appearance of the generated form.
Thanks for reading.