Zend Framework


Using recaptcha with Zend_Form

I was pleasantly surprised at how simple it was to integrate a good captcha solution with Zend_Form. Your form doesn’t have to extended a special object or anything; you just need to add a “captcha” element like so:

$this->addElement('captcha', 'captcha', array(
	'label' => 'Please Type:',
	'required' => true,
	'captcha' => array(
		'pubkey' => RECAPTCHA_PUBLIC_KEY,
		'privkey' => RECAPTCHA_PRIVATE_KEY,
		'captcha' => 'reCaptcha'
	)
));

Be sure to change or set the defined variables for public and private keys provided to you with your recaptcha account. You don’t have to do anything to validate if the user entered the correct letters in your controller as Zend has already taken care of that part for you. As long as you’re using Zend’s isValid() method like so:

if ($form->isValid($request->getPost())) {

Zend Form inline errors not displaying 1

If the inline errors from zend validator are not displaying after submitting your form, you may need to add the “Errors” decorator to the form like

//apply decorators to each element.
foreach ($elements as $fieldName => $element) {
       	$element->setDecorators(array('ViewHelper','Errors',array()));
}

Also be sure the validation is taking place on your form after submission. The controller should look something like this (use the isValid() method)

public function createNewUserAction()
{
$form = new User_Form_CreateUserForm();
if ($this->getRequest()->isPost()) {
	if ($form->isValid($this->getRequest()->getPost())) {
		$params = $form->getValues();
 
		//save the user
 
		$this->view->messages = array('User successfully saved.');
		$this->_redirect('user/user-admin');
	}
}
$this->view->form = $form;
}

Zend Framework: Action Helper by name Agent not found

I came across the following error today while reorganizing my zend framework application:
“Action Helper by name Agent not found”. This error occurred when I tried to use an action helper in a controller file. When it looked for the “redirector” helper, it decided to look for a helper named “_agent” instead.

How did I get here? Previously on this site, I was not using Zend_Auth, so I called php’s session_start() in the index file. I had to remove session_start() because I implemented Zend_Auth in one of the modules today. Apparently, helpers do not work if the session has not been started. Start the session by whichever method you choose and the error along with the missing helper issue will go away.


Zend Framework – how to autoload forms within a module 6

I got to thinking that it didn’t make sense to have a common /forms directory that would share forms between two modules when the forms would not be shared. A /forms directory within the specific module directory seemed more appropriate. The documentation on this isn’t very clear, but after some digging and trial and error I figured it out.

First, the directory structure.  Notice the location of the forms directories.

/Application

/modules

/default

/controllers

/forms

/views

/admin

/controllers

/forms

/views

First, add the two below lines to your application.ini file.

resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"
resources.modules[] = ""

Second, create a boostrap file for each module.  The file, named Bootstrap.php, should be placed in the root of the module directory and the class name should be {module name}_Boostrap. This bootstrap file will cause zend framework to automatically add the new forms directory to the autoloader.

class Admin_Bootstrap extends Zend_Application_Module_Bootstrap {}

Third, add a form class to the /forms directory. A login form would have a filename of Login.php and a class name of {module name}_Form_Login.

class Admin_Form_Login extends Zend_Form

Lastly, call your form from a controller file from within the same module.

$form = new Admin_Form_Login();

Be careful if you’re adding a Bootstrap.php file to the default module! From Zend:

The reason that the default module is skipped is because the documented use case – particularly how we setup an application in Zend_Tool – is that the default module is directly beneath application/ and contains the application bootstrap. If we then allow loading the default module’s bootstrap… we get into a recursive loop.


Zend_Auth “You cannot serialize or unserialize PDO instances”

I’m using Zend_Auth on a Zend Framework project and while working through an example where an object was written to the session using Zend_Auth ‘s getStorage()->write() method, I was given the following error:

“You cannot serialize or unserialize PDO instances”

Zend_Auth serializes arrays and objects when writing them to storage. Many built in PHP objects cannot be serialized, such as PDO. PHP’s serialize() method will call the __sleep method of the object you’re serializing prior to serialization, giving you the ability to clean up anything in the object that may cause problems (such as an object of PDO).

The solution was to implement the magic method __sleep in my object and return an array of the properties I wanted it to serialize like

	public function __sleep() {
		return array('id', 'name');
	}