Zend Framework

Using Callback Validator – Zend Framework

With Kohana v3, callback validators pretty standard. However, today is my first time to implement Callback validator in Zend Framework for my current project. Implementation is a bit different but my experience with Kohana’s callback validators give me a good grasp on it.

Making it short

To make this post short, I will also post my short code.

// add callback so that username must be unique
$this->setValidator('username', 'Callback', array(
	'breakChainOnFailure' => true,
	'options' => array(
		'callback' => array($this, 'usernameUnique')
	)
));

Where $this refers to the current object – a user model which has a method usernameUnique. It looks cryptic since the model is heavily pumped up with methods for modeling which is similar to Kohana’s Sprig but uses Zend Framework components such as Zend_Db, View Helpers and Zend_Validate components – including Zend_Validate_Callback.

To make it more clearer, the above code will get executed this way.

1. During validation, validators are loaded / initialized
2. When it is turn the ‘Callback’ validator, it is translated this way: Zend_Validate_ + validator name = Zend_Validate_Callback.
3. Zend_Validate_Callback is instantiated this way:

$v = new Zend_Validate_Callback(array(
	'callback' => array($userModelInstance, 'usernameUnique'))
);

4. The affected field is validated via the model.

And this is what my usernameUnique method looks like:

/**
 * Returns true if the username is unique / don't exists
 *
 * @param mixed $value
 * @return boolean
 */
public function usernameUnique($username)
{
	return !$this->userExists($username, false);
}

When the isValid() method is called on the Zend_Validate_Callback object, it calls the usernameUnique method passing the username value. Depending on the result, it will either pass or fail the validation.

The long story

To make it more clear, let’s expand the story more. Please do note that I just write the code below these paragraph on top of my head and all are not tested. Code below will only demonstrate as clear as possible as and as long as I can make it more clear – on how to use the callback validator, whether on Zend Framework of any other frameworks.

First, we are OOP and we are on modeling, so we will be using class callback instead of function. We will create an imaginary class Default_Model_User:

<?php

class Default_Model_User
{
	public function usernameUnique($username)
	{
		// check the username if it is unique
		// by checking it to the data source
		// for whatever method like database or web service
		
		$isUnique = routineToCheckIfUsernameIsReallyUnique($username);
		
		return $isUnique; 
	}
	
	public function isAllValid()
	{
		$v = new Zend_Validate_Callback(array(
			'callback' => array($this, 'usernameUnique'))
		);
			
		$username = 'lysender';
		
		if (!$v->isValid($username))
		{
			throw new Exception("$username is not unique");
		}
	}
}

// then we will use it
$model = new Default_Model_User;
$model->isAllValid();

When we call the imaginary method isAllValid(), it will instantiate the class Zend_Validate_Callback passing the parameters $this – which refers to the Default_Model_User object and ‘usernameUnique’ string which is the method to call.

When isValid() function of the validator is called, it will call the user model object’s usernameUnique() passing the username value. If the validation fails, it throws exception.

That’s it!

1 thought on “Using Callback Validator – Zend Framework”

  1. Thanks Lysender, this was really helpful. Shortly after implementing it, however, I came across the Zend_Validate_Db_NoRecordExists. Makes life even easier.

    I came from the Codeigniter framework (Very similar to Kohana) so had immediately looked for callbacks too!

Leave a reply

Your email address will not be published. Required fields are marked *