2010-07-26 4 views
0

저는 현재 헬퍼 함수와 setdefault 데코레이터를 추가하기 위해 제 자신의 Zend_Form 클래스를 만들었습니다.Zend Framework : Zend_Form을 확장하여 기본 Form 요소를 추가 하시겠습니까?

이제 기본 설정 (setMethod('post'))을 추가하고 일부 요소를 추가하는 방법 (스팸 봇을 방지하기 위해 허니팟 방법을 알고있는 경우)을 수행 할 수 있습니다. 어떻게해야합니까? __construct()에?

답변

2

작성자를 덮어 써야합니다.

class Your_Form extends Zend_Form 
{ 
    public function __construct($options = null) 
    { 
     $this->setMethod('POST'); 
     $this->addElement('hidden', 'hash', array('ignore' => true)); 
     parent::__construct($options); 
    } 

그래서 다른 양식 Your_Form를 확장하고 init() 전화, 그래서 일관성을 유지 할 수 있습니다.

class Model_Form_Login extends Your_Form 
{ 
    public function init() 
    { 
     $this->addElement('input', 'username'); 
     ... 

당신은 init() 덮어 쓸 경우 - 방법을 당신이 구조() __ 부모 :: 호출 할 필요가 없습니다 ...

class Your_Form extends Zend_Form 
{ 
    public function init() 
    { 
     $this->setMethod('POST'); 
     $this->addElement('hidden', 'hash', array('ignore' => true)); 

    } 

...하지만 모든 확장 양식에 있습니다 부모님 : init() 호출 이렇게

class Model_Form_Login extends Your_Form 
{ 
    public function init() 
    { 
     $this->addElement('input', 'username'); 
     ... 
     parent::init(); 
2

Zend_Form__construct을 읽으면 빈 방법 init()이 표시됩니다.
그래서 당신은 두 가지 선택이 있습니다

  1. 자신의 구조를 만들고 는 부모가 만드는 전화하는 것을 잊지 않습니다를
  2. (권장)는 init() 방법에서 원하는 무엇도했습니다. 그렇게하면 모든 것이 올바르게 인스턴스화되었는지 확인할 수 있습니다.

init()은 전체 프레임 워크 (일관되게 확장하려는 클래스)와 일치합니다.

class My_Super_Form extends Zend_Form{ 
    protected function init(){ 
    //here I do what ever I want, and I am sure it will be called in the instantiation. 
    } 
} 
+0

이유는 내가 "추상"젠드 양식 클래스 (재사용) 모든 다른 형태로 확장됩니다 만들려는 구조를 사용하기 때문에 생각합니다. 나는'__construct()'와 함께 동작하도록 만들고 맨 처음에는'parent :: construct()'를 호출했다. –

+0

Zend_Form을 상속받을 추상을 만들고 추상을 상속한다. 유일한 이유는 시스템을 업그레이드 할 때마다 라이브러리를 만져야한다는 것입니다. (또는 내가 한 일을 이해하지 못했다). –

관련 문제