Saturday, September 22, 2007

Try Zend Framework vol.3 "Extends Zend_View_Helper_Select"

Zend Framwrok has some useful helpers.
One of them is "Zend_View_Helper_Select".
Using it, you may set arguments like this.

controller/views/scripts/hoge/index.phtml
<?= $this->formSelect('hoge', null, null, array(''=>'', 'foo'=>'foo', 'bar'=>'bar')) ?>

You may not want to set blank as the default of options.

So, I extends Zend_View_Helper_FormSelect and add an argument to set default options.
library/Hoge/View/Helper/FormSelect.php
<php
require_once 'Zend/View/Helper/FormSelect.php';

class Hoge_View_Helper_FormSelect extends Zend_View_Helper_FormSelect
{
public function formSelect($name, $value = null, $attribs = null,
$options = null, $default_options = null, $listsep = "<br />\n")
{
$options = (is_array($default_options)) ? array_merge($default_options, $options) : $options;
return parent::formSelect($name, $value, $attribs, $options, $listsep);
}
}

It's just added argument for setting default options.
But, be careful it changes the number of arguments and changed order. :>

To use it.
<?= $this->formSelect('hoge', null, null, array('foo'=>'foo', 'bar'=>'bar'), array('' => '')) ?>

Simple right? :p
(weight 86.5kg BMI 30%)

Labels:

0 Comments:

Post a Comment

<< Home