Zend Framework: First Impressions
June 4, 2008 Posted by Al Castle
trackback
Categories: Castle, Puter Stuff, Web Design & Dev, gnash-teeth
Tags: form validation, frameworks, php, zend
I’ve been looking at various frameworks for PHP, including Symfony, CakePHP, CodeIgniter and Zend Framework. In theory these frameworks are supposed to make it easier to write and maintain code, by providing various degrees of structure and built-in libraries and helpers to do everything from sending email to validating user input.
Zend Framework is by far the loosest of all the frameworks, it’s structure is not so rigid and you’re free, to a certain extent to do things how you wish.
After having worked on trying to build some basic blocks which any application would require, I’m finding my hate level for frameworks building. For example the documentation and examples for Zend Framework are poor in my opinion. Being that Zend is the company which runs PHP, I would have expected php.net style user comments for each class. Those comments are where you usually find the real world examples, limitations, pitfalls and tricks.
One of my current woes is after building a registration and login form via Zend_Form I threw in some validation via the addValidator methods along with Zend_Auth to keep track of the logged in user and so on. As I worked I realized that there is no built in validator for checking something as basic as if a value in a database table is unique. In example, user registration - I would like to have the username and email address be unique. All I could find were basic checks for length, alphanumerics, regular expressions and other trivial string checks. Here’s an example of using Zend_Form.
public function loginForm()
{
$form = new Zend_Form();
$form->setAction('/user/login')
->setMethod('post');
// Create and configure username element:
$username = $form->createElement('text', 'username');
$username->class = 'formtext';
$username->setLabel('Username:');
$username->addValidator('alnum')
->addValidator('regex', false, array('/^[a-z]+/’))
->addValidator(’stringLength’, false, array(6, 20))
->setRequired(true)
->addFilter(’StringToLower’);
// Create & configure password element:
$password = $form->createElement(’password’,'password’);
$password->class = ‘formtext’;
$password->setLabel(’Password:’);
$password->addValidator(’StringLength’, false, array(6))
->setRequired(true);
// Add elements to form:
$form->addElement($username)
->addElement($password)
->addElement(’submit’, ‘login’, array(’label’ => ‘Login’));
return $form;
}
Upon posting one would normally do something like the following. isValid runs thru the addValidator checks, building the error messages as necessary.
if( !$registerForm->isValid($_POST))
{
// failed validation - return to register page with error messages
$this->view->registerForm = $registerForm;
return $this->render('register');
}
// form fields validate - database checks
$values = $registerForm->getValues();
While this is helpful I suppose, changing the error messages is an additional step. I found a page showing you how, but forgot to bookmark it as that’s the least of my concerns at this point.
To get back to my woe, how would I then integrate a custom database query as a validator? Well one method, (after much searching) is to write your own validation class(es). Or from other examples I’ve managed to find online (and anything of quality are few & far between), is to not define the validators during the form creation. Instead call each one after the POST, in this way the processing isn’t any different than how I would normally write my OO PHP code.
Something akin to this example.
public function registerAction
{
$request = $this->getRequest();
if ($request->isPost())
{
$errors = array();
$username = $request->getPost('username');
// validate 'username' length
$validator = new Zend_Validate_StringLength(8);
if ($validator->isValid($username)) {
// username is valid - do database query
// database query function goes here
} else {
// user name is invalid; save the reason
foreach ($validator->getMessages() as $messageId => $message) {
$errors['username'] = “Validation failure ‘$messageId’: $message”;
}
}
// make errors array available to your view
$this->view->errors = $errors;
}
}
Perhaps I’m simply expecting too much from this Framework. All the examples I’ve found show you how to use the validators when creating the forms as in my initial example. Why would the writers of the documentation and tutorials not show you a real world example instead? This would have saved me much time searching and some frustration. Again this is where the php.net style documentation which includes user submitted comments and code examples would have benefited me and probably many others.
While I freely admit that the writers of the Zend Framework and maintainers of PHP are far beyond my meager skills, I still have somehow managed to offer insight and improvement on the project with these humble examples and words.

Comments»
That seems like the functionality you would want on a general base class like Zend_Form. This gives you the ability to extend the class using something like Zend_db to do a lookup without altering the base class. In the end it provides a lot more flexibility. I would think anyway.
Based on what I have seen that the other frameworks have a ton of unnecessary complexity.
While I would agree in principle, you want basic classes you can build off of. This type of validation - checking a database - is so common, considering nearly every web application of any kind requires login forms and registration forms, that a built in validator would either already exist OR documentation AND/OR examples to do this type of Zend_Form database validation would exists on the class documentation pages in question. Furthermore, if it’s not possible to do database checks and use the form validators without extending the class, then why not detail this on the documentation?
I find that the examples don’t display information for this to be grossly incomplete, considering the commonality of this necessity.
I could go on with the documentation - today I realized that the examples for updating a row did not work. Additionally I eventually found forums where others have the same problem from following the documentation. A work around was suggested on the forum, which is the technique I ended up using.
My ultimate point being - if you do not have the time, inclination or know how to keep the documentation up to date and extensive enough for newbies to follow - then allow moderated user comments.
[...] it provides me, I did after all choose it over the other frameworks I’ve mentioned in a previous post. It’s not as if I can’t write my own either, but simply that I find the absence of full [...]
Hi,
regarding user registration i do it similar to this:
http://zfsite.andreinikolov.com/2008/06/part-5/
You can join #zftalk on FreeNode to talk about everything related to the Zend Framework
Best wishes