簡體   English   中英

zf2,表單集合沒有在zf2中創建正確的輸入名稱

[英]zf2, Form collections not creating correct input name in zf2

我正在嘗試創建,用於顏色輸入字段的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手冊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 $_POST['hash']獲得一個顏色名稱。

為什么zf2沒有打印<input type="text" value="" class="input-mini" required="required" name="colors[0][hash]"> 請告訴我的代碼有什么問題。

哦,我終於找到了答案。 我得打電話

$form->prepare();

在視圖中渲染任何內容之前。 現在它有效

我可以證實。 這段代碼對我有用:

// prepare form is needed to properly display Collection fields
$form->prepare();

echo $this->form()->openTag($form);

// echo other normal form fields

echo $this->formCollection($form->get('colors'));

echo $this->form()->closeTag();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM