How to set onSuccess[] in a presenter for a form multiplied in a component?

netteman
Member | 126
+
0
-

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 :)

CZechBoY
Member | 3608
+
0
-

I would add new event on component and store there presenter commands.

https://doc.nette.org/…s/form-reuse

netteman
Member | 126
+
0
-

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
}
CZechBoY
Member | 3608
+
0
-

That is almost event. The difference is that the compinent’s property is array and you call that property in onSuccess form event callback.

netteman
Member | 126
+
0
-

Ok, I'll have a look at the events. Thanks for your help :)