getPresenter() method on forms first instance returns NULL value
- alnux
- Member | 139
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 | 405
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.
- alnux
- Member | 139
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
Last edited by alnux (2015-02-27 04:21)