2012-05-11 2 views
0

Zend Framework에서 사용자가 선택할 수있는 항목이 하나 뿐인 경우 숨겨진 요소로 자동 전환되는 select 요소를 만들려고합니다. 나는 다음과 같은 사용하여 클래스를 확장해야한다는 것을 알 수 있도록 내가 더 이상의 값이 있으면 그냥 선택 요소처럼 행동 할 :Zend_Form_Element_Select를 값에 따라 Zend_Form_Element_Hidden으로 변경하십시오.

class Application_Form_Element_SingleSelect extends Zend_Form_Element_Select{} 

하지만 출력에 활용하는 방법을 잘 모르겠어요 숨겨진 요소로. 당신이 그 요소에 추가 된 모든 장식을 통해 HTML을 생성하는 책임이 방법을 렌더링 오버라이드 (override) 할 필요가

public function render(Zend_View_Interface $view = null){ 
    $options = $this->getMultiOptions(); 

    // check to see if there is only one option 
    if(count($options)!=1){ 
     // render the view 
     return parent::render($view); 
    } 

    // start building up the hidden element 
    $returnVal = '<input type="hidden" name="' . $this->getName() . '" '; 

    // set the current value 
    $keys = array_keys($options); 
    $returnVal .= 'value="' . $keys[0] . '" '; 

    // get the attributes 
    $attribs = $this->getAttribs(); 

    // check to see if this has a class 
    if(array_key_exists('class', $attribs)){ 
     $returnVal .= 'class="' . $attribs['class'] . '" '; 
    } 

    // check to see if this has an id 
    if(array_key_exists('id', $attribs)){ 
     $returnVal .= 'id="' . $attribs['id'] . '" '; 
    } else { 
     $returnVal .= 'id="' . $this->getName() . '" '; 
    } 

    return $returnVal . '>'; 
} 

답변

1

:

업데이트이 내가 생각 해낸 최종 코드이었다.

class Application_Form_Element_SingleSelect extends Zend_Form_Element_Select{ 

public function render(Zend_View_Interface $view = null) 
{ 
    $options = $this->getMultiOptions(); 
    return count($options) > 1 ? parent::render($view) : '' ; 
} 

} 
관련 문제