开发者

Zend Framework: Extend Zend_Form to add default Form elements?

i have created m开发者_如何学Pythony own Zend_Form class currently just to add some helper functions and setdefault decorators.

now what i want to do is add some defaults (setMethod('post')) and add some elements (if u know the honeypot method to prevent spam bots.) how do i do it? in the __construct()?


Overwrite the contructor should be fine.

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

So your other Forms can extend Your_Form and call init(), so it stays consistent.

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

If you overwrite the init()-Method you don't have to call the parent::__construct()...

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

    }

... but all your extending Forms have to call parent::init() like so

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


If you will read the __construct of Zend_Form you will see it is calling the empty method init().
So you have two choices:

  1. create your own construct and do not forget to call the parent construct
  2. (recommended) put what ever you want in the init() method. That way you will be sure that everything was instantiated correctly.

the init() is something consistent to the entire framework (which ever class you want to extend).

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜