getPresenter() method on forms first instance returns NULL value

Notice: This thread is very old.
alnux
Member | 106
+
0
-

I have this case, im using forms on separate file from presenter but when i try to call

$this->form->getPresenter()

on first instance of calling component from presenter

class PrivilegePresenter extends AclBasePresenter
{
	/**  @inject @var \App\Common\Classes\FormsContainer */
	public $forms;

protected function createComponentNewEdit()
    {
        return $this->forms->getAclFactory()->privilegeForm();
    }
}

it give me a NULL presenter but when i push button to process, the getPresenter() methods works fine returning me the presenter.

Here My form class

class AclForms extends BaseForm
{
    public function privilegeForm()
    {
        dump($this->form->getPresenter());// it returns NULL value
		$this->form->onSuccess[] = callback($this, 'process');
        return $this->form;
	}

	public function process()
    {
		dump($this->form->getPresenter()); // this returns the presenter
	}
}

And my form container

namespace App\Common\Classes;

use App\Common\Forms\Src as CF; // Common forms container
use App\Backend\Acl\Forms\Src as ACL; // Acl Forms container
/**
 * Container to find all forms
 */
class FormsContainer {

    public function getUserRegistry()
    {
        return new CF\UserRegistryForms();
    }

    public function getAclFactory()
    {
        return new ACL\AclForms();
    }
}

Right now im passing the presenter object on form component calling

protected function createComponentNewEdit()
{
    return $this->forms->getAclFactory($this)->privilegeForm();
}

But i want to know if there is a way to call presenter without this solution

Last edited by alnux (2015-02-26 17:46)

greeny
Member | 406
+
+1
-

Presenter is attached to form later (see documentation ). All of the code manipulating with presenter should be situated after the form has presenter attached. You can monitor for presenter by using monitor and then solve your needs in attached

IMHO, you should not need Presenter in Form. Stuff from presenter should be passed in your factory function (privilegeForm in your case) and register success callback from presenter:

$presenter = $this;
$form->onSuccess[] = function (Form $form, $values) use ($presenter) {
	$presenter->redirect("something", ['id' => $values->id]);
}

This way, you can get rid of using Presenter in Forms. It makes your forms reusable.

Comments

alnux:

my model and other vars are injected on my presenter, that is becouse i want to call presenter on form build. For example

class AclForms extends BaseForm{

	public function privilegeForm()
    {
        $privileges = $this->presenter->model->getTreeValues($this->presenter->getParameter('postId')); // getTreeValues() value method can be NULL or some postId
        $this->form->addSelect('parent_id', 'Parent id', $privileges, 15)
          ->setRequired('forms.privilege.required.parent');

		$this->form->onSuccess[] = callback($this, 'process');
        return $this->form;
	}

	public function process()
	{
		//here is not the problem
		.....
		.....
	}
}

my base form

abstract class BaseForm extends Object{

    protected $form;

    public function __construct()
    {
        $this->form = new Form;

        $this->form->addProtection('The form was broken please try again');
    }
.......
....
...
...
}

that is becouse i want to call presenter on build function. So i dont have clear how to use monitor

5 years ago
greeny:

Well you can inject your model into form (its IMHO correct approach). Use constructor or create factory for your component (in this case form)

5 years ago