作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直在谷歌搜索无济于事。我的一个表单中有一个多复选框表单元素。这是我用来创建它的代码:
$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/
我是一名优秀的程序员,十分优秀!