Snippet does not invalidate on AJAX Request if you put it in included latte file

Notice: This thread is very old.

6 years ago

selmaq
Member | 2
+
0
-

Hi there!

I guess i found a bug or maybe i miss here something. But thing is that if you structure you template into several files and use {include ‘filename.latte’} latte macros for including additional files into your template, than you will expierence problem with your snippets. They wont invalidate on AJAX requests.

Lets say you have:

@layout.latte

<!DOCTYPE html>
<html>
	<head> ... </head>
	<body>
		{include header.latte}
		{include main.latte}
		{include footer.latte}

		{block scripts}
		<script src="{$basePath}/js/vendor/jquery.js"></script>
		<script src="{$basePath}/js/vendor/netteForms.js"></script>
		<script src="{$basePath}/js/vendor/nette.ajax.js"></script>
		{/block}
	</body>
</html>

main.latte

{form subscribeForm class => ajax}
	{snippet errors}
	<div n:if="$control[subscribeForm]->ownErrors">
		<p n:foreach="$control[subscribeForm]->ownErrors as $error">{$error}</p>
	</div>
	{/snippet}
	...
	{label email /}
	{input email}
	{input send}
{/form}

BasePresenter.php

protected function createComponentSubscribeForm()
{
	$form = new Form;
	$form->addText('email', 'E-mail:')->addRule(Form::EMAIL, 'Please enter your e-mail.');
	$form->addSubmit('send', 'Submit');
	$form->onSuccess[] = $this->subscribeFormSuccess;
	return $form;
}

public function subscribeFormSuccess($form)
{
	if ($form->isSubmitted() && $form->isValid())
	{
		$values = $form->getValues();

		$this->database->table('emails')->insert(array('email' => $values->email));
	}
	else
	{
		$form->addError('E-mail address is incorrect!');
	}

	if ($this->isAjax()) $this->redrawControl('errors');
	else $this->redirect('this');
}

After AJAX request you end up with this response:

{"state":[]}

Once you put the contents of the main.latte into @layout.latte directly it will work correctly and you get snippet update:

{"state":[],"snippets":{"snippet--errors":"\t\t\t\t<div>\n\t\t\t\t\t<p>E-mail address is incorrect!<\/p>\n\t\t\t\t<\/div>\n"}}

So i guess this is a bug or it has to do something with variable scope or i don't know. But i just came here to let you know. Maybe you experienced similar problem and didn't know how to solve it.

Cya

6 years ago

Oli
Member | 1219
+
0
-

I Think You cannot use snippet in a form. You have to wrap The form by snippet.

6 years ago

selmaq
Member | 2
+
0
-

Well i made a quick test. I wrapped the form into the snippet and it didn't work either. Maybe i have to pass some parameters into the main.latte template like this:

{include main.latte param=$param, ...}

But i am not sure what parameters to put there.