gpt4 book ai didi

php - zf2,表单集合未在 zf2 中创建正确的输入名称

转载 作者:可可西里 更新时间:2023-11-01 00:05:46 26 4
gpt4 key购买 nike

我正在尝试为颜色输入字段创建 html 集合 .. 它将使用 javascript 动态添加

我的 ColorFieldset代码是

namespace Dashboard\Form;

use Zend\Form\Fieldset;
use Zend\InputFilter\InputFilterProviderInterface;

class ColorFieldset extends Fieldset implements InputFilterProviderInterface
{
public function __construct()
{

parent::__construct('color');

$this->add(array(
'name' => 'hash',
'options' => array(
'label' => 'Color'
),
'attributes' => array(
'required' => 'required',
'class' => 'input-mini'
)
));
}

/**
* @return array
\*/
public function getInputFilterSpecification()
{
return array(
'hash' => array(
'required' => true,
)
);
}
}

并将其添加到表单中

$this->add(array(
'type' => 'Zend\Form\Element\Collection',
'name' => 'colors',
'options' => array(
'count' => 2 ,
'should_create_template' => true,
'allow_add' => true,
'target_element' => array(
'type' => 'Dashboard\Form\ColorFieldset'
)
)
));

在我看来文件.. colors.phtml

<div id="colors_container" class="">
<?php echo $this->formCollection( $form->get('colors')); ?>
</div>

它打印输出像

<div class="" id="colors_container">

<label><span>Color</span><input type="text" value="" class="input-mini" required="required" name="hash"></label>

<label><span>Color</span><input type="text" value="" class="input-mini" required="required" name="hash"></label>

<span data-template='<label><span>Color</span><input name="hash" required="required" class="input-mini" type="text" value=""></label>'></span>

</div>

它应该像 .. 一样打印,这在 zf2 manual 2.0 中有解释。

<div class="" id="colors_container">

<label><span>Color</span><input type="text" value="" class="input-mini" required="required" name="colors[0][hash]"></label>

<label><span>Color</span><input type="text" value="" class="input-mini" required="required" name="colors[1][hash]"></label>

<span data-template='<label><span>Color</span><input name="colors[__index__][hash]" required="required" class="input-mini" type="text" value=""></label>'></span>

</div>

我希望 html 输入名称为 colors[__index__][hash] .但它打印名称为 <input type="text" value="" class="input-mini" required="required" name="hash"> .

在上面的例子中。我只会在帖子中得到一个颜色名称 $_POST['hash'] .

为什么 zf2 不打印 <input type="text" value="" class="input-mini" required="required" name="colors[0][hash]"> ?请告知我的代码中有什么问题。

最佳答案

哦,终于自己找到答案了。我得打电话

$form->prepare();

在 View 中渲染任何东西之前。现在可以了

关于php - zf2,表单集合未在 zf2 中创建正确的输入名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17229822/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com