php 属性 自定义,选择选项中的php – zend表单自定义属性?

使用ZF的Zend_Form_Element_Select实现是不可能的.您需要创建自己的元素.我做了类似的事情,这里是相关代码:

require_once 'Zend/Form/Element/Select.php';

/**

* Select, but with the possibility to add attributes to s

* @author Dominik Marczuk

*/

class Zend_Form_Element_SelectAttribs extends Zend_Form_Element {

public $options = array();

public $helper = 'selectAttribs';

/**

* Adds a new

* @param string $value value (key) used internally

* @param string $label label that is shown to the user

* @param array $attribs additional attributes

*/

public function addOption ($value,$label = '',$attribs = array()) {

$value = (string) $value;

if (!empty($label)) $label = (string) $label;

else $label = $value;

$this->options[$value] = array(

'value' => $value,

'label' => $label

) + $attribs;

return $this;

}

}

把它放到/library/Zend/Form/Element/SelectAttribs.php中.您还需要一个助手来渲染元素.把它放到你的view helpers目录下,把它命名为SelectAttribs.php.这是我的文件的内容:

require_once 'Zend/View/Helper/FormElement.php';

class Zend_View_Helper_SelectAttribs extends Zend_View_Helper_FormElement {

public function selectAttribs($name, $value = null, $attribs = null, $options = null, $listsep = "
\n") {

$info = $this->_getInfo($name, $value, $attribs, $options, $listsep);

extract($info); // name, id, value, attribs, options, listsep, disable

// force $value to array so we can compare multiple values to multiple

// options; also ensure it's a string for comparison purposes.

$value = array_map('strval', (array) $value);

// now start building the XHTML.

$disabled = '';

if (true === $disable) {

$disabled = ' disabled="disabled"';

}

// Build the surrounding select element first.

$xhtml = '

. ' name="' . $this->view->escape($name) . '"'

. ' id="' . $this->view->escape($id) . '"'

. $disabled

. $this->_htmlAttribs($attribs)

. ">\n ";

// build the list of options

$list = array();

$translator = $this->getTranslator();

foreach ($options as $opt_value => $option) {

$opt_disable = '';

if (is_array($disable) && in_array($opt_value, $disable)) {

$opt_disable = ' disabled="disabled"';

}

$list[] = $this->_build($option, $disabled);

}

// add the options to the xhtml and close the select

$xhtml .= implode("\n ", $list) . "\n

";

return $xhtml;

}

protected function _build($option, $disabled) {

$html = '

foreach ($option as $attrib => $value) {

$html .= " $attrib=\"$value\"";

}

return $html.$disabled.">".$option['label']."

";

}

}

有了这个,你应该准备好了:

$elt = new Zend_Form_Element_SelectAttribs('whatever');

$elt->addOption($value,$label,array('attribname' => 'attribvalue'));

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值