Make generic nette redirect after ajax form succeeded

EmanuelNeu
Member | 6
+
0
-

Hello world!

I have typical sign up form in signUpControl component with ajax (naja). I want to redirect to Sign:in presenter after the signUpForm is successfully submited. I can redirect with javascript using payload but than flashmessages doesnt work… when i submit this form withot ajax, my loading animation on form doesn't work…

so i want to call typical nette redirect after ajax form was succesfully submited… is it even possible ?

//this will work if the form was not subbmited with ajax
public functon signUpFormSucceeded(Form $form, stdClass $values): void
{
	//save user data

	$this->getPresenter()->redirect('Sign:in');
}


//this work with ajax but flasshmessages doesnt work...
public functon signUpFormSucceeded(Form $form, stdClass $values): void
{
	//save user data

	$this->getPresenter()->getPayload()->redirect = $this->presenter->link('Sign:in');
}

//than in js:

naja.addEventListener("success", (event) => {
  if (event.detail.payload.hasOwnProperty("redirect")) {
    naja.redirectHandler.makeRedirect(event.detail.payload.redirect, true);
  }
});

thanks, have a nice day
Neumann.

dakur
Member | 493
+
0
-

Well, you can probably take the flash message ID and append it to the URL manually if nothing else works.

Have you tried also redirect event according to docs?

EmanuelNeu
Member | 6
+
0
-

Solution was simple, I don't know why it took me so long to figure it out.

add attribute data-naja-force-redirect to the form and than call $this->getPresenter()->redirect('Sign:in');

simple as that.

m.brecher
Generous Backer | 873
+
0
-

@EmanuelNeu

instead of data-naja-force-redirect html attribute and this code:

naja.addEventListener("success", (event) => {
  if (event.detail.payload.hasOwnProperty("redirect")) {
    naja.redirectHandler.makeRedirect(event.detail.payload.redirect, true);
  }
});

try this code:

naja.redirectHandler.addEventListener('redirect', (event) => event.detail.setHardRedirect(true))