gpt4 book ai didi

zend-framework2 - ZF2 Form\Element\MultiCheckbox : how to get each item on a new line?

转载 作者:行者123 更新时间:2023-12-01 08:59:12 25 4
gpt4 key购买 nike

我一直在谷歌搜索无济于事。我的一个表单中有一个多复选框表单元素。这是我用来创建它的代码:

$this->add(array (
'name' => 'thingyId',
'type' => 'MultiCheckbox',
'options' => array (
'value_options' => $thingyArray,
)
));

在我的 View 脚本中,我有这个:

<?= $this->formRow($form->get('thingyId')); ?>

表单元素显示良好,但所有复选框都在一行上。如何让每个复选框都在新行上?

最佳答案

如果您查看此 link ,您可以看到第四个参数是部分的。因此,您可以使用多种方法来完成任务。

方法一:

echo $this->formRow($element, null, null, 'template-file');

现在,创建一个名为 template-file.phtml 的模板文件,以随心所欲地渲染元素。

//template-file.phtml
<span><?php echo $label; ?></span><br/>
<?php foreach ($element->getValueOptions() as $key => $value): ?>
<input type="checkbox" name="<?php echo $element->getName() ?>[]" value="<?php echo $value; ?>">
<span><?php echo $key; ?></span><br/>
<?php endforeach; ?>

方法二

通过扩展默认助手创建您自己的 View 助手。

namespace Application\View\Helper;

class MyFormRow extends \Zend\Form\View\Helper\FormRow
{
/**
* @var string
*/
protected $partial = 'template-file';
}

现在,通知我们的应用程序您的模块中的新助手,

namespace Application;

class Module
{
public function getViewHelperConfig()
{
return array(
'invokables' => array(
'myFormRow' => 'Application\View\Helper\MyFormRow'
)
);
}
}

最后使用助手:

echo $this->myFormRow($element);

关于zend-framework2 - ZF2 Form\Element\MultiCheckbox : how to get each item on a new line?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21799446/

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