Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
jkavalik opened this issue Mar 25, 2020 · 4 comments
Open

Comments

@jkavalik
Copy link

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
Copy link
Member

MartkCz commented Mar 30, 2020

This is old nette syntax, the correct is:

public function createComponentAbcForm() : AbcForm {
        $form = new AbcForm();
        // ...
}

@MartkCz
Copy link
Member

MartkCz commented Apr 5, 2020

Probably fixed by e3a337b

@jkavalik
Copy link
Author

jkavalik commented Apr 5, 2020

@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
Copy link

slischka commented Apr 13, 2020

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants