zend-form select optgroup,如何指定id

发布于 2024-12-01 16:59:08 字数 1198 浏览 1 评论 0 原文

您好,我正在使用 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 10.11.

谢谢

Hello i am using Zend Framework Form and have tried to get this example to work http://framework.zend.com/issues/browse/ZF-8252, but it fails xD

this is my code

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

);

$agenti->addMultiOptions($options);

and the generated code is :

<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>

where id="agente_id-optgroup-Agenti attivi: " is not xhtml valid Line 724, Column 44: value of attribute "id" must be a single token

i am using zend 1.11.10

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

欢烬 2024-12-08 16:59:09

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

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

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

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

在布局的顶部

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

Zend is usually pretty good about rendering the proper html, given a doctype.

Try setting your doctype like this if you aren't already.

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

AND

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

at the top of your layout

I don't have a install of ZF i can mess with easy, if this doesn't work ill setup a test environment.

長街聽風 2024-12-08 16:59:08

创建一个扩展核心 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 = "
    \n") { $info = $this->_getInfo($name, $value, $attribs, $options, $listsep); 提取($信息); // 名称、id、值、属性、选项、listep、禁用 // 将 $value 强制为数组,以便我们可以将多个值与多个值进行比较 // 选项;还要确保它是一个用于比较目的的字符串。 $value = array_map('strval', (array) $value); // 检查元素是否可以有多个值 $multiple = ''; if (substr($name, -2) == '[]') { // 名称隐含多个 $multiple = ' multiple="multiple"'; } if (isset($attribs['multiple'])) { // 属性集 if ($attribs['多个']) { // 真实属性;设置多个属性 $multiple = ' multiple="multiple"'; // 确保名称指示允许多个值 if (!empty($multiple) && (substr($name, -2) != '[]')) { $name .= '[]'; } } 别的 { // 错误属性;确保属性未设置 $multiple = ''; } 取消设置($attribs['多个']); } // 现在开始构建 XHTML。 $禁用=''; if (true === $disable) { $disabled = 'disabled='disabled''; } // 首先构建周围的选择元素。 $xhtml = '<选择' 。 ' name="' . $this->view->escape($name) . '"' 。 ' id="' . $this->view->escape($id) . '"' 。 $多个 。 $禁用 。 $this->_htmlAttribs($attribs) 。 “>\n”; // 构建选项列表 $列表=数组(); $translator = $this->getTranslator(); foreach ((array) $options as $opt_value => $opt_label) { 如果(is_array($ opt_label)){ $opt_disable = ''; if (is_array($disable) && in_array($opt_value, $disable)) { $opt_disable = '禁用=“禁用”'; } if (null !== $translator) { $opt_value = $translator->translate($opt_value); } $opt_id = ' id="' . $this->formatElementId($id . '-optgroup-' . $opt_value) . '"'; $list[] = ''; foreach ($opt_label as $val => $lab) { $list[] = $this->_build($val, $lab, $value, $disable); } $list[] = ''; } 别的 { $list[] = $this->_build($opt_value, $opt_label, $value, $disable); } } // 将选项添加到 xhtml 并关闭选择 $xhtml .= implode("\n ", $list) 。 "\n"; 返回$xhtml; } 私有函数 formatElementId($id) { // 在这里放置您想要的 id 值的任何过滤器 $id = trim(strtr($id, array('[' => '-', ']' => '', ' ' => '', ':' => '')), '-'); $id = strtolower($id); 返回$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',
        )
    )
));

Create a custom view helper FormSelect that extends the core FormSelect and then modify the code.

  1. Include the path to your view helpers in the bootstrap file

protected function _initHelpers()
{
    $this->bootstrap('view');
    $view = $this->getResource('view');
    $view->addHelperPath('My/View/Helper', 'My_View_Helper');
}
  1. The custom view helper. It's a copy of Zend_View_Helper_FormSelect but with small modification.

    class My_View_Helper_FormSelect extends 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;
    }
    

    }

Done. Create multi select element with a valid 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',
        )
    )
));
荆棘i 2024-12-08 16:59:08

试试这个,它对我有用:

$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: "

try this, it's works for me:

$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

and the result is:

<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>

the problem is that the id attribute does not accept spaces and special special characters:

id="agente_id-optgroup-Agenti attivi: "
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文