В форме:
<?php
require_once 'glx/Form/Element/Select.php'; // custom select class
// ... in init or __create function :
$categories = new Model_DbTable_Categories(); // some Model
$PID = new glx_Form_Element_Select('PID'); // custom select object
$PID
->setLabel('PID')
->setDecorators(array('ViewHelper'))
->addMultiOptions($categories->getSelectOptions())
;
Файловая библиотека / glx / Form / Select.php:
<?php
require_once 'Zend/Form/Element/Multi.php';
$error_reporting = error_reporting(0);
@include_once '../application/views/helpers/glxFormSelect.php'; // first, maby here
if (! class_exists('Zend_View_Helper_glxFormSelect'))
require_once 'glx/View/Helper/glxFormSelect.php'; // or least, maby here
error_reporting($error_reporting);
class glx_Form_Element_Select extends Zend_Form_Element_Multi
{
public $helper = 'glxFormSelect'; // this is my custom code
}
?>
Файл приложения / views / helpers / glxFormSelect.php или библиотека / glx / View / Helpe / glxFormSelect.php:
<?php
require_once 'Zend/View/Helper/FormElement.php';
class Zend_View_Helper_glxFormSelect extends Zend_View_Helper_FormSelect
{
public function glxFormSelect($name, $value = null, $attribs = null, $options = null, $listsep = "<br />\n")
{
return parent::formSelect($name, $value, $attribs, $options, $listsep);
}
protected function _build($value, $label, $selected, $disable)
{
if (is_bool($disable))
$disable = array();
$oldLabel = $label; // this is my custom code
$label = ltrim($label); // this is my custom code
$opt = '<option'
. ' value="' . $this->view->escape($value) . '"'
. ' label="' . $this->view->escape($label) . '"';
if (($countSpaces = strlen($oldLabel) - strlen($label)) > 0) // this is my custom code
$opt.= sprintf(' style="padding-left:%dpx"', (15 * $countSpaces)); // this is my custom code
if (in_array((string) $value, $selected))
$opt .= ' selected="selected"';
if (in_array($value, $disable))
$opt .= ' disabled="disabled"';
$opt .= '>' . $this->view->escape($label) . "</option>";
return $opt;
}
}
?>
И окончательный HTML-код результата (добавлен стиль с отступом влево):
<select name="PID" id="PID">
<option value="1" label="Categories" style="padding-left:15px">Categories</option>
<option value="2" label="Publications" style="padding-left:30px">Publications</option>
<option value="83" label="Links" style="padding-left:45px">Links</option>
...