HTMx and Nette Forms combination idea

shuseisajdi
Member | 9
+
0
-

While trying to a simple way to do “HTMx” without “naja” or “snippet,
I have devised a way for combining Nette Forms and HTMx, and it is working successfully.
However, I am concerned about the use of handleDisregard() as a dummy function.
Is there a more efficient way to achieve the same way?

HomePresenter.php:

	public function createComponentSearchForm(): Form
	{
		$form = new Form;
		$form->addProtection();
		$form->addText('search', 'Search:');
		$form->onSuccess[] = [$this, 'searchFormSucceeded'];
		$form->onError[] = [$this, 'searchFormError'];
		return $form;
	}

	public function searchFormSucceeded(Form $form, stdClass $values):void
	{
		$payload = "<h2>Replacement Contents</h2>";
		$this->sendResponse(new Nette\Application\Responses\TextResponse($payload));
	}

	public function searchFormError(){
		$this->error('Form security error', 403);
	}

	public function handleDisregard(){}

index.latte:

	<form n:name="searchForm">
		<input id="search" n:name="search" type="text" required autocomplete="off"
			hx-post="{link handleDisregard!}"
			hx-trigger="keyup changed delay:500ms"
			hx-headers='{"X-Requested-With": "XMLHttpRequest"}'
			hx-target="#result"
			hx-swap="innerHTML"
			hx-validate="true">
	</form>
	<div id="result"></div>