How to set onSuccess[] in a presenter for a form multiplied in a component?
- netteman
- Member | 125
Hi :)
I had a form in a component and I could set the onSuccess[] in a presenter that was creating the component like this:
//component xyz
protected function createComponentTheForm()
{
$form = new Nette\Application\UI\Form;
$form->addHidden('id', 1);
$form->addSubmit('submit', 'Submit');
return $form;
}
//presenter
public function createComponentXyz()
{
$component = $this->xyzControlFactory->create();
$component->getComponent('theForm')->onSuccess[] = [$this, 'presenterMethod'];
}
Now I added multiplier to the form in the component and I don't know how to set the onSuccess[] from the presenter because when I try to dump the $component->getComponent(‘theForm’) I get an object of Nette\Application\UI\Multiplier which doesn't have the onSuccess[] property
//new component
protected function createComponentTheForm()
{
return new Multiplier(function ($id) {
$form = new Nette\Application\UI\Form;
$form->addHidden('id', $id);
$form->addSubmit('submit', 'Submit');
return $form;
});
}
Is there any way how to create a multiplied form in a component and set the onSuccess[] in a presenter?
Thanks :)
- netteman
- Member | 125
I'm not familiar with component events but I'll google them.
Meanwhile, I found out this code works:
//component xyz
public $presenterMethod;
...
protected function createComponentTheForm()
{
return new Multiplier(function ($id) {
$form = new Nette\Application\UI\Form;
$form->addHidden('id', $id);
$form->addSubmit('submit', 'Submit');
$form->onSuccess[] = $this->presenterMethod;
return $form;
});
}
//presenter
public function createComponentXyz()
{
$component = $this->xyzControlFactory->create();
$component->presenterMethod = [$this, 'theFormSuccessMethod'];
}
public function theFormSuccessMethod(Form $form, $values)
{
//do stuff
//flash message
//redirect
}