code 303 header on forms with errors

alnux
Member | 139
+
0
-

hello real artisans,
I would like your help on how I can send the 303 code in the headers when a form returns with errors.
Right now I have it set as follows

use Nette;
use Nette\Application\UI\Form;

class BasePresenter extends Nette\Application\UI\Presenter
{
	public $translator;

    public function startup()
	{
		parent::startup();

        // ----BEGIN -- This is is for Hotwire Turbo forms redirection
		if($this->getHttpRequest()->getPost()) {
			$this->getHttpResponse()->setCode(303);
		}
        // ---- END --------------------------------------------------
	}
}

but it is applied to all the forms sent, but i want that the just with error requeriments conditions puts the 303 header.
thanks for your help

Rick Strafy
Nette Blogger | 81
+
+1
-

If you have some FormFactory (service where you construct your base Form that you use everywhere) just inject there an response and add function to onError, for example

$form->onError[] = function (Form $form): void { $this->response->setCode(303) }

If you dont, then create a service FormFactory, and everywhere where you do new Form().. just inject FormFactory in constructor and construct form with $form = $this->formFactory->create(), and in that create() you'll set that onError callback

alnux
Member | 139
+
0
-

Rick Strafy wrote:

If you have some FormFactory (service where you construct your base Form that you use everywhere) just inject there an response and add function to onError, for example

$form->onError[] = function (Form $form): void { $this->response->setCode(303) }

If you dont, then create a service FormFactory, and everywhere where you do new Form().. just inject FormFactory in constructor and construct form with $form = $this->formFactory->create(), and in that create() you'll set that onError callback

thanks @RickStrafy s for the information, since it works.

Last edited by alnux (2023-08-15 16:39)