2011-07-29 4 views
1

하나의 요소 인 것처럼 젠드 양식에 요소 그룹을 추가하는 방법이 있다면 궁금합니다. 서브 폼과 비슷하게 보입니다.하지만 하위 폼의 기능이 너무 많을 수 있습니다.양식에 요소 그룹을 추가하는 방법이 있습니까?

여기에 제 사례가 있습니다. 여러 페이지 양식을 처리하는 클래스를 만들었습니다. 폼의 페이지를 기반으로 폼의 맨 아래에있는 버튼을 변경하는 논리를 작성하고 싶습니다. Form Page 1Form Page 2Form page 3

나는 원래 젠드 - 양식 - DisplayGroup 내 문제를 해결할 것이라고 생각하지만, 먼저 폼에 항목을 추가 한 다음 표시 그룹에 추가해야하고 통해 표시 그룹을 전달할 수 없습니다 요소가 첨부 된 함수. 나는 요소의 배열을 사용하는 아이디어는 단지의 양식에 요소의 배열을 추가하는 지금 뭔가 다른 반대 논리를 추가로 나를 때리는

public function setSubmitButtonGroup($submitButtonGroupElements) 
{ 
    /*Code to set button group*/ 
} 

같은 것 기능을 가지고 싶습니다 렌더링 ...하지만 누구든지 "더 나은"아이디어를 가지고 있거나 전에 이것을 했습니까? 사람이 궁금하면

BTW ... 내가 느슨하게이 섹션 떨어져 내 초기 디자인을 내놓고있어 : Zend Framework Advance Form Usage.

답변

0

그래서 내 문제의 복잡성이 오는 버튼 조작을 단행. 배열과 위에서 언급 한 addElements()를 사용하면 도움이됩니다.

간단한 대답

내 문제에 대한 대답은 양식이 말하자면하지만 addElements를 사용하여 양식에 추가 할 수 있도록 렌더링되기 전에하는 "내장"후 조작 할 수있는 배열을 (했다).

긴 대답

전체 그림을 얻으려면, 당신은 다음 또는 이전 버튼을 누르면 때마다, 당신은 하위 폼의 배열을 통해 통과하는 상상. 이 경우 버튼 렌더링을 처리하는 함수가 필요합니다. 나는 세계 (부모 클래스 Form_MultiPage에서 재사용되지 않음)에서 가장 구현이 아니다하지만, 경우에 한 Statment를 사용하여 종료하지만, 일 :

을 내 mulipage 폼 클래스의 내 확장자에 내가

public function setSubmitControls() 
{ 
    $previous = new Zend_Form_Element_Submit('previous',array(
     'label'=>'previous', 
     'required'=>false, 
     'ignore'=>false, 
     'order'=>9000 
    )); 
    $cancel = new Zend_Form_Element_Submit('cancel',array(
     'label'=>'Cancel', 
     'required'=>false, 
     'ignore'=>false, 
     'order'=>9003 
    )); 
    $next = new Zend_Form_Element_Submit('next',array(
     'label'=>'Next', 
     'required'=>false, 
     'ignore'=>false, 
     'order'=>9002 
    )); 
    $finished = new Zend_Form_Element_submit('finish',array(
     'label'=>'Finish', 
     'required'=>false, 
     'ignore'=>false, 
     'order'=>9004 
    )); 
    $submitControls = array(); 
    echo var_dump($this->getCurrentSubForm()->getName()); 
    switch($this->getCurrentSubForm()->getName()) 
    { 
     case 'billInfo': 
      $submitControls = array(
       $next, 
       $cancel 
      ); 
     break; 
     case 'payerInfo': 
      $submitControls = array(
       $previous, 
       $next, 
       $cancel 
      ); 
     break; 
//So on for other subforms 
    } 
    $this->setSubmitButtonGroup($submitControls); 
} 
이 내 부모 클래스에서

, Form_Multipage, 나는

public function setSubmitButtonGroup(array $elements) 
{ 
    $this->_submitButton = $elements; 
} 

그리고

public function addSubmitButtonGroupToSubForm(Zend_Form_SubForm $subForm) 
{ 
    $subForm->addElements($this->_submitButton); 
    return $subForm; 
} 
이 나는 빛나는이 기능

public function prepareSubForm($spec) 
{ 
    if (is_string($spec)) { 
     $subForm = $this->{$spec}; 
    } elseif ($spec instanceof Zend_Form_SubForm) { 
     $subForm = $spec; 
    } else { 
     throw new Exception('Invalid argument passed to ' . 
          __FUNCTION__ . '()'); 
    } 
    $subform = $this->setSubFormDecorators($subForm); 
    $subform = $this->addSubmitButtonGroupToSubForm($subForm); 
    $subform = $this->addSubFormActions($subForm); 
    $subform->setMethod($this->getMethod()); 
    return $subForm; 
} 
1

당신은 자신에게 세 PARAMS를받을 공장, 양식 요소, 현재의 컨트롤러를 만들 수 있으며, 현재의 행동. 그런 다음 해당 팩토리에서 컨트롤러/액션 조합을 기반으로 빌더를 호출하고 양식을 전달할 수 있습니다.

작성자에서 다른 구성 요소에 저장된 해당 컨트롤러/동작 요구 사항에 따라 1, 2 또는 3 버튼을 추가합니다. 작업이 완료되면 양식을 공장으로 반품하고 공장은 양식을 반환합니다. 당신은 당신이 추상 클래스와 메소드 buildForm에서 필요로하는 모든 다른 compoments를 초기화 할 수있는 instanciation을 자동화 해주는하려면

My_Form // your Zend_Form object 

My_Form_Factory // Your new factory (see below) 

My_Form_Factory_Builder_Controller_Action // One of your builder (see below) 

My_Form_Factory_Component // Extends the corresponding Zend_Form_Elements 

// basic factory that can be called like My_Factory::factory($form, $controller, $action) 
class My_Form_Factory { 
    static public function factory($form, $controller, $action) 
     $builderClass = "My_Form_Factory_Builder_" . $controller . '_' . $action; 
     $builder = new $builderClass($form); 
     return $builder->buildForm(); 
} 

// Basic builder 
class My_Form_Factory_Builder_Controller_Action 
{ 
    protected $_form; 
    protected $_previousComponent ; 
    protected $_nextComponent ; 
    protected $_cancelComponent ; 

    public function __construct($form) 
    { 
     $this->_form = $form; 
     $this->_previousComponent = new My_Form_Factory_Component_Previous(); 
     $this->_nextComponent = new My_Form_Factory_Component_Next(); 
     $this->_cancelComponent = new My_Form_Factory_Component_Cancel(); 
    } 

    public function buildForm() 
    { 
     $this->_form->addElement($previousCompnent); 
     $this->_form->addElement($nextComponent); 
     $this->_form->addElement($cancelComponent); 

     return $this->_form; 
    } 
} 

()는 당신이 현재의 인터페이스에 필요한 요소를 추가 할 수 있습니다. (필자는 이러한 종류의 "마법"에 의존하는 것보다는 각 빌더에서 코드를 반복하는 것이 좋지만 가능한 방법입니다).

+0

oooooo ...와 양식의 "페이지"를 렌더링 할 때 호출되는 414,. 그게 어디 까지나 도전 할 수있는 곳인지 알 수 있어요. 그러나 그것은 또한 내가하고있는 방식을 완전히 재 설계해야 할 것입니다. 그러나 당신의 아이디어는 단순하고 쉽고 간단합니다. –

1

잘 모르겠습니다. 올바르게 문제를 이해하고 있지만 어떻게해야 할 지 잘 알고 있습니다.

Zend_Form 개체에서 배열에`addElements ($ elements) '를 가진 그룹으로 요소를 추가 할 수 있습니다. Submit 버튼 등등에서 $ elements 배열을 가져온 클래스가 있고 난 다음에는 간단히 pop을 띄웁니다. 또한 displayGroup을 추가하지만 버튼이있는 곳을 제어하기 위해 별도로 간단하게 추가합니다. 양식은 객체이기 때문에 다음과 같은 간단한 작업을 수행 할 수 있지만 항상 내 의도를 표시하는 참조를 추가합니다.

업데이트는 다음 다중 형태의 어떤 페이지를 알고 함께

function addButtons(&$form,$nextName = null) { 
    $buttons = $this->getButtons(); // this will be an array with your buttons 
    // make sure you have the element names in your buttons arrays 
    if (is_string($nextName)) { 
     $buttons['nextButton']->setLabel($nextName); 
    } elseif (is_bool($nextName) && false === $nextName) { 
     unset($buttons['nextButton']; 
    } 
    // repeat for other buttons 

    $form->addElements($buttons); 
    $elementNames = array_keys($buttons); 
    $form->addDisplayGroup($elementNames,'buttonGroup',array('legend'=>'Click some buttons')); 

} 

$this->addButtons($form,'Finish'); 
+0

'addElements()'함수를 가져 주셔서 감사합니다. 나는 그것을 완전히 잊어 버렸습니다 ... 예, 저는 여러분이 여기있는 것과 매우 흡사 한 생각을하고 있습니다. 거의 복잡하지는 않지만 (불필요하게 복잡하지는 않습니다). 나는 기본적으로'setSubmitButtonGroup (array $ elements)'라는 함수로'addElements()'를 구현했다. 나는 지금 내 자신의 질문에 바로 대답 할 수 없다. 좋은 아이디어를 가져 주셔서 감사합니다! –

+0

도와 드리겠습니다. 복잡성은 수요와 함께;) –

관련 문제