0

젠드 양식 요소 렌더링에 약간의 문제가 있습니다. 우리는 현재 Zendframework 2가 설치된 설문 조사 관리 플랫폼을 구축 중이며 매트릭스 이미지 (예 : 링크 된 이미지)로 렌더링되는 Questiontype이 필요합니다.ZF2 매트릭스로 라디오 요소 렌더링

https://imageshack.com/i/ndynulp

I -

(그래서 2 개 블루 라인의 오른쪽에있는 모든 것을 멀리 던져 질 수 있고 다른 테이블에 추가 할 수 그것은 또한 단지 테이블에 하나 개의 질문이 아니라이 될 수 있음) 현재 양식의 모든 요소가 물론 데이터베이스에서 동적으로 추가되므로이 문제를 가장 잘 해결하는 방법을 알지 못합니다.

이렇게 여러 개의 라디오 버튼을 표시하는 가장 좋은 해결책은 무엇입니까?

현재 사용자 정의 양식 요소를 작성하려고하지만 아래 그림과 같이 뷰 도우미에게 요소를 렌더링하도록 알려주지 않습니다. (또는 어떻게 렌더링할지 알려주는 방법도 있습니다. 내 요소).

답변

1

좋아, 나는 도움을 주셔서 감사합니다. 도와 주셔서 감사합니다. :-)

다른 사람이 이러한 문제에 직면 들어, I는 다음과 같이하여 해결 :

폼 요소 (크기 Atribute가 렌더링되는 라디오 버튼 표 제목의 양이다).

<?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 
     )); 
    } 

} 

그리고이 요소의 렌더링에 사용됩니다 radiomatrix.phtml입니다 : 나는 내 자신의 뷰를 작성하는 경우 그래서

<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> 
+0

다행은 그것을 <3 고정 –

0

이 :-) 당신의 도움을 위해

덕분에 PHTML 파일

당신이 당신의 PHTML 내부 코드를 사용하고 사용하여 그것을 자기

렌더링 할에 기본 zf2보기 렌더러 수행 할 수 없습니다 양식 필드에 반복과 같은 복잡한 형태

를 구축하는 foreach는

기본 $ this-> formElement() zf2보기에 도움이되지 수

+0

을 내 phtml 파일에서 $ this-> formMatrix()와 함께 사용하는 렌더러가 가능합니까? 그렇다면, 그러한 뷰 헬퍼를 작성하는 가장 좋은 해결책은 무엇입니까? 귀하의 빠른 답변 주셔서 감사합니다 :-) – Velixir