Displaying two fields in a row
- Ondřej Brejla
- Member | 746
Have a look at examples/Forms/manual-rendering.php
in the
distribution pack.
- unagi2020
- Member | 18
Thanks for the reply. I have seen manual rendering example from distribution
pack and I am now confused.
What is the best practice of rendering manual forms ?
Should the form be constructed and processed outside Nette framework or should be processed back in action class. I am having tough time understanding which section of code should go in action, component and template.
Please Help!!!
- Jan Tvrdík
- Nette guru | 2595
Normally, the form definition and the submit handler are in presenter (some programmers prefer to extend AppForm instead but that doesn't have to bother you).
<?php
use Nette\Application\AppForm as Form; // or Nette\Application\UI\Form in the latest night build
final class FooPresenter extends BasePresenter
{
protected function createComponentLoginForm() // createComponent<componentName>($name)
{
$form = new Form();
$form->addText('login', 'Login');
$form->addPassword('password', 'Password');
$form->addSubmit('send', 'Sign in');
$form->onSubmit[] = callback($this, 'loginFormSubmitted');
return $form;
}
public function loginFormSubmitted(Form $form)
{
$values = $form->getValues();
// ...
$this->flashMessage('You have been successfully logged in.');
$this->redirect('Homepage:'); // there should be always redirect after successful form sending
}
}
The most simple way to render a form is by using
{control loginForm}
in template. This can be customized by
modifying $form->getRenderer()->wrappers
but the level of
customization is limited.
Complete manual rendering is also possible. The most comfortable way of doing it is by using FormMacros and there is no need to move the form definition outside of presenter. Just register the macros
abstract class BasePresenter extends Nette\Application\Presenter
{
...
public function templatePrepareFilters($template)
{
$template->registerFilter($latte = new Nette\Templates\LatteFilter());
JanTvrdik\Templates\FormMacros::register($latte->getHandler());
}
}
and use them in template.