Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Extending a baseform

Hey,

I'm trying to extend a baseform with all other form's. Extending the baseform does not execute the initialize function and add the required fields to the form. It works with parent::initialize(), but i was wondering if there's any option without having to call it in every form.

class BaseForm extends Form
{

    function initialize()
    {   
        $this->add(new Hidden($this->security->getTokenKey(), array('value' => $this->security->getToken())));
    }

}

Thanks, Timmeyy



22.3k
Accepted
answer

How about this:

use Phalcon\Forms\Form;

class BaseForm extends Form
{
    public function initialize()
    {
        $this->add(new Hidden($this->security->getTokenKey(), array('value' => $this->security->getToken())));
    }
}

and your normal forms:

class ContactForm extends BaseForm
{
    // No initialize = calls the parent initialize
}

or

class OtherForm extends BaseForm
{
    public function initialize()
    {
        parent::initialize();

        // Add more stuff here
    }
}


440

Well thats what I have now, so I guess there is no way around it :)

I don't think you can do it any other way really. The OtherForm::initialize() overrides the BaseForm::initialize() so you have to instruct your code to call the initialize model.