2013-06-21 2 views
3

Я пытаюсь создать, коллекция HTML для цветов поля ввода .. который будет динамически добавляемая с помощью JavaScriptZF2, Форма коллекция не создающая правильное имя входа в ZF2

Моего 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]">? Пожалуйста, советьте, что не так в моем коде.

+0

хороший вопрос ... thank u –

ответ

10

Ой, наконец, нашел ответ сам. Я должен позвонить

$form->prepare(); 

перед тем, как сделать что-нибудь на виду. теперь он работает

+0

thank u! я спас свое время ... у меня была точная проблема –

+0

Я потратил столько времени на это, прежде чем найти свой ответ. Жаль, что я не посмотрел раньше. Благодаря! – MrNorm

+0

damm, потерянный день для этого, и затем найдите этот ответ. и я поддерживаю это раньше. лол . –

0

могу подтвердить. Этот код работал для меня:

// 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();