I have a little problem with the rendering of the Zend Form Elements. We are currently building a Survey Administration platform with Zendframework 2 and therefore need Questiontypes that are rendered in a matrix style (like in the linked image).
(It could also just be one question and not 2 in a table - so everything to the right of the 2 blue lines could be thrown away and be added in another table)
https://imageshack.com/i/ndynulp
I currently don't know how to solve this problem the best because all the Elements in the Form are of course added dynamically coming from the database.
What would be the best solution for showing multiple Radio Buttons like this?
At the moment I'm trying to write a Custom Form Element but I don't exactly unterstand how I tell the View Helper to render my Element like seen in the Image below (or even how I tell him how to render my element).
Thanks for your help :-)
Ok I managed to do it by writing my own view Helper, thanks for the Help @Sina. :-)
For anyone else facing this problem, I solved it like the following:
The Form Element: (The size Atribute is the Amount of radio buttons and Table headings that will be rendered).
<?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;
}
}
This is the View Helper Class:
<?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
));
}
}
And this is the radiomatrix.phtml that will be used to render the 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>