zend-form select optgroup,如何指定id

问题描述 投票:0回答:3

您好,我正在使用 Zend Framework Form 并尝试让这个示例正常工作 http://framework.zend.com/issues/browse/ZF-8252,但它失败了 xD

这是我的代码

$options = Array
(
    [] => Qualsiasi Agente
    [agenti_attivi] => Array
        (
            [4] => Giovanni Abc
            [10] => Luigi Abc
            [13] => Michela Abc
        )

);

$agenti->addMultiOptions($options);

生成的代码是:

<select name="agente_id" id="agente_id" tabindex="6">
    <option value="" label="Qualsiasi Agente" selected="selected">Qualsiasi Agente</option>
    <optgroup id="agente_id-optgroup-Agenti attivi: " label="Agenti attivi: ">
    <option value="4" label="Giovanni Abc">Giovanni Abc</option>
    <option value="10" label="Luigi Capoarea">Luigi Abc</option>
    <option value="13" label="Michela Abc">Michela Abc</option>
    </optgroup>

</select>

其中 id="agente_id-optgroup-Agenti attivi: " 不是 xhtml 有效 第 724 行,第 44 列:属性“id”的值必须是单个标记

我正在使用 zend 1.11.10

谢谢

php zend-framework zend-form
3个回答
5
投票

创建一个扩展核心 FormSelect 的自定义视图助手 FormSelect,然后修改代码。

  1. 在引导文件中包含视图助手的路径

protected function _initHelpers()
{
    $this->bootstrap('view');
    $view = $this->getResource('view');
    $view->addHelperPath('My/View/Helper', 'My_View_Helper');
}
  1. 自定义视图助手。它是 Zend_View_Helper_FormSelect 的副本,但做了一些小修改。

    类 My_View_Helper_FormSelect 扩展 Zend_View_Helper_FormSelect {

    public function formSelect($name, $value = null, $attribs = null,
        $options = null, $listsep = "<br />\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);
    
        // check if element may have multiple values
        $multiple = '';
    
        if (substr($name, -2) == '[]') {
            // multiple implied by the name
            $multiple = ' multiple="multiple"';
        }
    
        if (isset($attribs['multiple'])) {
            // Attribute set
            if ($attribs['multiple']) {
                // True attribute; set multiple attribute
                $multiple = ' multiple="multiple"';
    
                // Make sure name indicates multiple values are allowed
                if (!empty($multiple) && (substr($name, -2) != '[]')) {
                    $name .= '[]';
                }
            } else {
                // False attribute; ensure attribute not set
                $multiple = '';
            }
            unset($attribs['multiple']);
        }
    
        // now start building the XHTML.
        $disabled = '';
        if (true === $disable) {
            $disabled = ' disabled="disabled"';
        }
    
        // Build the surrounding select element first.
        $xhtml = '<select'
                . ' name="' . $this->view->escape($name) . '"'
                . ' id="' . $this->view->escape($id) . '"'
                . $multiple
                . $disabled
                . $this->_htmlAttribs($attribs)
                . ">\n    ";
    
        // build the list of options
        $list       = array();
        $translator = $this->getTranslator();
        foreach ((array) $options as $opt_value => $opt_label) {
            if (is_array($opt_label)) {
                $opt_disable = '';
                if (is_array($disable) && in_array($opt_value, $disable)) {
                    $opt_disable = ' disabled="disabled"';
                }
                if (null !== $translator) {
                    $opt_value = $translator->translate($opt_value);
                }
                $opt_id = ' id="' . $this->formatElementId($id . '-optgroup-' . $opt_value) . '"';
                $list[] = '<optgroup'
                        . $opt_disable
                        . $opt_id
                        . ' label="' . $this->view->escape($opt_value) .'">';
                foreach ($opt_label as $val => $lab) {
                    $list[] = $this->_build($val, $lab, $value, $disable);
                }
                $list[] = '</optgroup>';
            } else {
                $list[] = $this->_build($opt_value, $opt_label, $value, $disable);
            }
        }
    
        // add the options to the xhtml and close the select
        $xhtml .= implode("\n    ", $list) . "\n</select>";
    
        return $xhtml;
    }
    
    private function formatElementId($id)
    {
        // in here put whatever filter you want for the id value
        $id = trim(strtr($id, array('[' => '-', ']' => '', ' ' => '', ':' => '')), '-');
        $id = strtolower($id);
        return $id;
    }
    

    }

完成。创建具有有效 id 的多选元素。

<?php
$this->addElement('multiSelect', 'agente_id', array(
    'label' => 'Label Name:',
    'multiOptions' => array(
        '' => 'Qualsiasi Agente',
        'Agenti attivi: ' => array(
            4 => 'Giovanni Verdi',
            10 => 'Luigi Capoarea',
            13 => 'Michela Passarin',
        )
    )
));

2
投票

试试这个,它对我有用:

$select = new Zend_Form_Element_Select('select');
$options = Array(
    '' => 'Qualsiasi Agente',
    'agenti_attivi' => Array(
            4 => 'Giovanni Verdi',
            10 => 'Luigi Capoarea',
            13 => 'Michela Passarin'
        )
);
$this->addElements(array($xxxx,$select,$yyyy)); // $this : the form instance

结果是:

<select id="select" name="select">
  <option label="Qualsiasi Agente" value="">Qualsiasi Agente</option>
  <optgroup label="agenti_attivi">
    <option label="Giovanni Verdi" value="4">Giovanni Verdi</option>
    <option label="Luigi Capoarea" value="10">Luigi Capoarea</option>
    <option label="Michela Passarin" value="13">Michela Passarin</option>
  </optgroup>
</select>

问题在于 id 属性不接受空格和特殊字符:

id="agente_id-optgroup-Agenti attivi: "

0
投票

Zend 通常非常擅长在给定文档类型的情况下渲染正确的 html。

如果您还没有这样做,请尝试像这样设置您的文档类型。



<?php
$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
$viewRenderer->initView();
$viewRenderer->view->doctype('XHTML1_STRICT');

<?php
$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
$viewRenderer->initView();
$viewRenderer->view->doctype('XHTML1_STRICT');

并且



<?php echo $this->doctype(); ?>

在布局的顶部

我没有安装 ZF,我可以轻松搞乱,如果这不起作用,请设置一个测试环境。

© www.soinside.com 2019 - 2024. All rights reserved.