A note on CAPTCHA decorators and Zend_Form_…

So if you have been struggling with setting up decorators with Zend_Form_Element_Captcha, here’s a note.

I used to get an additional textbox with the hash returned by Zend creating the captcha image. And if you were stumped as to how to remove them from your display try the following.

First of all your form element please note that I extracted this from my extension of the Zend_Form class

$captcha = new Zend_Form_Element_Captcha('captcha',
 array('label' => 'Type in the text you see in the image',
 'captcha' => array('captcha' => 'Image',
 'wordLen' => 6,
 'timeout' => 300,
 'height' => 60,
 'width' => 250,
 'font' => APPLICATION_PATH . '/../public/assets/fonts/arial.ttf',
 'fontSize' => 30,
 'imgDir' => APPLICATION_PATH . '/../public/assets/captcha/',
 'imgUrl' => 'http://' . $_SERVER['HTTP_HOST'] . '/assets/captcha/',
 ))
 );// there are certainly more options here but not necessary for my purposes.
//You can try altering the noise levels in the captcha image such as how many
// dots and lines should be created wiuth the image
// google noise levels in Zend_Form_Element_Captcha

This generates my captcha and I can add it to my form by going with

$this->addElements(array($email,$captcha,$submit));

Bear in mind that $name and $email and $submit are also new instances of their respective Zend_Form elements. In other words don’t include them if you’re copying and pasting.

Now after adding your elements you have to define a separate decorator for the Captcha because it’s obviously different from other form elements so here goes:

$captcha->setDecorators(array(
 'Captcha',
 'Errors',
 array('Label', array('separator' => '<br />', 'requiredPrefix' => '* ')),
 array('HtmlTag', array('tag' => 'p', 'class' => 'form-element'))
 )
 );

Hope it’s helpful

Having multiple instances of Zend Navigation in a single View

so I have been playing around with the Zend Framework rather extensively over the past few weeks and it has been a very rewarding experience.

I am posting this here because it was a bit difficult finding a work around for the issue I describe below.

Zend Navigation had been giving me a lot of problems especially because I could not get the Navigation object passed to my view object to render two different navigation configs. The top menu which remains the same for all pages and is rendered in my layout and other sub menus rendered in my individual view pages.

Now getting to it:

in my bootstrap I initialise my navigation with something like this.

$config = new Zend_Config_Xml(APPLICATION_PATH . '/configs/navigation.xml', 'nav');
 $miscNavConfig = new Zend_Config_Xml(APPLICATION_PATH . '/configs/miscnavigation.xml', 'nav');
 $navigation = new Zend_Navigation($config);
 $miscNavigation = new Zend_Navigation($miscNavConfig);
 Zend_Registry::set(Zend_Navigation, $navigation);
 Zend_Registry::set('MiscNavigation', $miscNavigation);
 $view->navigation($navigation);

Now  lines 2, 4, and 6 are the new lines I have added for additional nagivation. I beleive you should be able to add new configurations as you move along if you need to.

The next bit was retrieving the sub navigation in my controller:

$miscNavigation = Zend_Registry::get('MiscNavigation');
$this->view->miscnavigation = $miscNavigation;

You can do this in your view but because I route several of my views through this controller I want to have to do it only once.

And finally in my view:

$options = array('ulClass' => 'submenu');
echo($this->navigation()->menu()->renderMenu($this->miscnavigation, $options));

Bear in mind that I use the renderMenu method instead of passing $this->miscnavigation into the menu method. If you do that you will overwrite any other instances of menu you have on the page with the miscnavigation object.