jump to navigation

Numerous Tests Results
June 4, 2008  Posted by Al Castle

3comments Categories: Castle, Squirrels, TimeWaster   Tags: , , , ,

According to this test, I could take up to 29 five year olds in a fight. Provided I fight dirty, use them as a weapon, gouge out their little eyes, etc.

29

In this test we discover I’m only 75% geek. The other 25% is sex machine.
75% Geek

Most of my friends can feel somewhat safe. As there’s less than a 50% chance I’d eat you if I was starving.
44%

Share/Save/Bookmark

Zend Framework: First Impressions
June 4, 2008  Posted by Al Castle

4comments Categories: Castle, Puter Stuff, Web Design & Dev, gnash-teeth   Tags: , , ,

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.

Share/Save/Bookmark