0

i have created my 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()?

2 Answers 2

3

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();
Sign up to request clarification or add additional context in comments.

Comments

3

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.
   }
}

2 Comments

the reason why i think of using the construct is because i want to create an "abstract" zend form class (reusability) where all my other forms will extend. i mannaged to make it work with __construct() and i called parent::construct() at the start too
create the abstract which will inherit the Zend_Form and you inherit the abstract. The only reason for that is that each upgrade of the system you will have to touch the library if you do that. (or I did not understand what you did).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.