我正在嘗試創(chuàng)建,用于顏色輸入字段的html集合..這將使用javascript動(dòng)態(tài)添加
我的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>
它應(yīng)該打印像…在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輸入名稱為顏色[__ index __] [hash].但它打印名稱為< input type =“text”value =“”class =“input-mini”required =“required”name =“hash”> .
在上面的例子中.我只會(huì)在post $_POST [‘hash’]中獲得一個(gè)顏色名稱.
為什么zf2不打印< input type =“text”value =“”class =“input-mini”required =“required”name =“colors [0] [hash]”> ?請(qǐng)告訴我的代碼有什么問(wèn)題.
解決方法:
哦,我終于找到了答案.我得打電話
$form->prepare();
在視圖中渲染任何內(nèi)容之前.現(xiàn)在它有效
來(lái)源:https://www.icode9.com/content-1-337601.html聯(lián)系客服