Zend framework2 ZF2将无线电元素渲染为矩阵

Zend framework2 ZF2将无线电元素渲染为矩阵,zend-framework2,custom-component,radiobuttonlist,zend-form-element,Zend Framework2,Custom Component,Radiobuttonlist,Zend Form Element,我对Zend表单元素的渲染有点问题。我们目前正在使用Zendframework 2构建一个调查管理平台,因此需要以矩阵样式呈现的问题类型(如链接图像) (它也可能只是一个问题,而不是一张表中的两个问题——因此,两条蓝线右侧的所有问题都可以扔掉并添加到另一张表中) 我目前不知道如何最好地解决这个问题,因为表单中的所有元素当然都是从数据库动态添加的 显示这样多个单选按钮的最佳解决方案是什么 目前,我正试图编写一个自定义表单元素,但我并不完全理解我如何告诉视图助手渲染我的元素,如下图所示(甚至我如何

我对Zend表单元素的渲染有点问题。我们目前正在使用Zendframework 2构建一个调查管理平台,因此需要以矩阵样式呈现的问题类型(如链接图像)

(它也可能只是一个问题,而不是一张表中的两个问题——因此,两条蓝线右侧的所有问题都可以扔掉并添加到另一张表中)

我目前不知道如何最好地解决这个问题,因为表单中的所有元素当然都是从数据库动态添加的

显示这样多个单选按钮的最佳解决方案是什么

目前,我正试图编写一个自定义表单元素,但我并不完全理解我如何告诉视图助手渲染我的元素,如下图所示(甚至我如何告诉他如何渲染我的元素)


感谢您的帮助:-)

这不能通过phtml文件中的默认zf2视图渲染器来完成

您必须在phtml中使用代码,并将其呈现为您自己的代码

通过使用foreach在表单字段中迭代并构建如此复杂的表单


默认值$this->formElement()无法在zf2视图中提供帮助

好的,我通过编写自己的视图帮助程序实现了这一点,谢谢@Sina.:-)

对于其他面临此问题的人,我按如下方式解决了此问题:

Form元素:(size Atribute是将呈现的单选按钮和表标题的数量)


那么,如果我编写自己的视图渲染器,并在phtml文件中与$this->formMatrix()一起使用,这可能吗?如果是,编写这样一个视图助手的最佳解决方案是什么?感谢您的快速回答:-)
<?php

/**
 * @namespace
 */

namespace ExecuteSurvey\Form\Element;

/**
 * @uses Zend\Form\Element
 */
use Zend\Form\Element;

/**
 * Custom Form Element that is here for rendering a Matrix of Radio Elements
 * @category ExecuteSurvey
 * @package ExecuteSurvey_Form
 * @subpackage ExecuteSurvey_Form_Element
 * @author Dominik Einkemmer
 */
class RadioMatrix extends Element {

    protected $attribute = array(
        'type' => 'radiomatrix'
    );

    private $labels = array();

    private $size;

    public function setSize($size){
        if($size == 0 || !is_numeric($size)){
            throw new Exception("Size can't be 0 and has to be numeric");
        }
        $this->size = $size;
    }

    public function setLabels(array $labels){
        if(!is_array($labels)){
            throw new \Zend\Form\Exception\InvalidArgumentException("Array expected but ".gettype($labels)." given.");
        }
        foreach($labels as $label){
            $this->labels[] = $label;
        }
    }

    public function getLabels() {
        return $this->labels;
    }

    public function getSize() {
        return $this->size;
    }
}
<?php

/**
 * @namespace
 */

namespace ExecuteSurvey\Form\View\Helper;

/**
 * @uses Zend\Form\ElementInterface
 * @uses Zend\Form\View\helper\AbstractHelper
 */
use Zend\Form\ElementInterface;
use Zend\Form\View\Helper\AbstractHelper;

/**
 * Class FormRadioMatrix which renders the actual element through a view
 * @category ExecuteSurvey
 * @package ExecuteSurvey_Form
 * @subpackage ExecuteSurvey_Form_View
 * @subpackage ExecuteSurvey_Form_View_Helper
 */
class FormRadioMatrix extends AbstractHelper {

    /**
     * Path to the .phtml file
     * @var string
     */
    protected $script = 'execute-survey/form-element/radiomatrix';

    /**
     * Method that renders the View Element
     * @param \Zend\Form\ElementInterface $element
     * @return \Zend\View\Renderer\RendererInterface
     */
    public function __invoke(ElementInterface $element) {
        return $this->getView()->render($this->script, array(
                    'element' => $element
        ));
    }

}
<div class="table-responsive">
    <table class="table">
        <thead>
            <tr>
                <th></th>
                <?php for ($i = 1; $i <= $element->getSize(); $i++): ?>
                    <th><?php echo $i ?></th>
                <?php endfor; ?>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($element->getLabels() as $key => $label): ?>
                    <tr>
                        <td><?php echo $label?></td>
                        <?php for($j = 1;$j<=$element->getSize();$j++):?>
                        <td><input type="radio" value="<?php echo $j?>" name="<?php echo $label.'X'.$key?>" id="<?php echo $element->getName().'X'.$key.'X'.$j?>"></td>
                        <?php endfor;?>
                    </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
</div>