Skip to content
Viliam Kopecký edited this page May 30, 2017 · 4 revisions

How to use forms

See the directory theme/forms.

Then, in your Latte templates, you can access it via global $Forms variable by the filename. So the theme/forms/contact.php will be in $Forms['contact'].

Then in the template, you can output it simply like this: {$Forms[contact]}.

Important thing is, to return the form in each definition file.

See the Nette Forms documentation.

$form = new Nette\Forms\Form;

// frm container prevents collisions with WordPress namespace.
$c = $form->addContainer('frm');

// then add inputs to the container.
$c->addText('email', 'Your email')
	->addCondition($form::FILLED)
		->addRule($form::EMAIL, 'Please fill in a valid e-mail address.');

$c->addTextarea('message', 'Message')
	->setRequired('Please fill in a message.');

$c->addSubmit('send', 'Send');

$form->onValidate[] = function($form) {
	if(rand(0, 100) > 50) {
		$form->addError('Random error');
	}
};

// This is MangoPress function to validate a form
if(isFormValid($form, __FILE__)) {
	dump($c->getValues());
}

// Don't forget to return the form in the end!
return $form;

Forms in Latte

{form $Forms[contact]}
	{formContainer frm}
		<ul class="form-errors" n:if="count($form->getOwnErrors())">
			<li n:foreach="$form->getOwnErrors() as $error">{$error}</li>
		</ul>
		<div class="form-control">
			{label email}
			{input email}
			<div class="form-control-error" n:ifcontent>{inputError email}</div>
		</div>
		<div class="form-control">
			{label message}
			{input message}
			<div class="form-control-error" n:ifcontent>{inputError message}</div>
		</div>
		<div class="form-control">
			{input send}
		</div>
	{/formContainer}
{/form}
Clone this wiki locally