contributte / forms-multiplier

:repeat: Form multiplier & replicator for Nette Framework
https://contributte.org/packages/contributte/forms-multiplier.html
MIT License
26 stars 21 forks source link

Add/remove buttons do not work when form is attached in constructor #47

Open jkavalik opened 4 years ago

jkavalik commented 4 years ago

The multiplier does not work when we create our form like this:

public function createComponentAbcForm(string $name) : AbcForm
    {
        $form = new AbcForm($this, $name);
        $form->createElements($this->getAction());
        $form->onSuccess[] = [$this, "baseFormSuccess"];
        return $form;
    }

imho because the form is already attached in here so the onAnchor is never called

if ($obj instanceof \Nette\Application\UI\Form) {
    $obj->onAnchor[] = function (): void {
    $this->whenAttached();
    };
}
$obj->onRender[] = function (): void {
    $this->whenAttached();
};

and the call during onRender is too late for the buttons to react properly and the form goes to onSuccess instead (the submittedBy is never set by the buttons in this case).

I tried fixing it by calling whenAttached() directly when the form is already anchored but that broke something else and the add button was not rendered at all because it was not yet configured.

MartkCz commented 4 years ago

This is old nette syntax, the correct is:

public function createComponentAbcForm() : AbcForm {
        $form = new AbcForm();
        // ...
}
MartkCz commented 4 years ago

Probably fixed by https://github.com/contributte/forms-multiplier/commit/e3a337bfeb44eaab99b808ba7fa3eb9bae87b36b

jkavalik commented 4 years ago

@MartkCz I tried that already, does not work for us either, because we have createElements as a separate function call after construct, so when whenAttached() is called in this case, there is nothing in the multiplier yet. It would have to be some event after createComponent but before handleSignal/fireEvents.. No idea what that could be.

On the other hand removing $this (presenter) from the constructor seems to work fine atm. And moving createElements to constructor before parent::__construct() would be probably acceptable too as we only use the multiplier in a couple of special components and can cover that with some comments to explain the difference to other code.

slischka commented 4 years ago

Hi there. I have same problem. The attached method is called before I am able to add create and remove button. Its called in contributte/forms-multiplier/src/Multiplier.php:516 when the multiplier is assign to the form. I have one kinda solution, but its not super sexy. https://github.com/slischka/forms-multiplier/commit/461ea71a9efb92bd64bda5d45b6e5268919b4c8d

I was thinking about to add some logic to addCreateButton but its to complex. The result would be multiple calling getHttpData etc. nad that would not be good.