php中表单名称未定义,php – zf2,表单集合没有在zf2中创建正确的输入名称

本文探讨了在ZF2框架中实现颜色输入字段时遇到的问题。作者试图通过创建ColorFieldset类来动态添加颜色输入字段,并希望每个字段的名称能够正确地反映其在集合中的位置。然而,实际输出不符合预期,经过调试发现,在渲染视图前调用$form->prepare()解决了该问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我正在尝试创建,用于颜色输入字段的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

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

它的打印输出就像

Color

Color

Color

Color

我希望html输入名称为颜色[__ 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();

在视图中渲染任何内容之前.现在它有效来源:https://www.icode9.com/content-1-337601.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值