Create forms as nested factories:
+- IFormFactory ---+ +- IFormFactory --+ +- IFormFactory -+
| Base factory | -> | Adds inputs | -> | Adds mapping | -> create() : Form
+------------------+ +-----------------+ +----------------+
Benefits:
- Change form configuration around the application on one place.
- Each factory can be used independently.
- It is not necessary to inherit forms.
The best way to install Venne/Forms is using Composer:
composer require venne/forms:@dev
example:
services:
basicFormFactory:
class: Nette\Application\UI\Form
arguments: [NULL, NULL]
implement: Venne\Forms\IFormFactory
setup:
- setRenderer(@system.formRenderer)
- setTranslator(@translator.translator)
autowired: no
Implement Venne\Forms\IFormFactory
:
class FooFormFactory implements Venne\Forms\IFormFactory
{
private $formFactory;
public function __construct(IFormFactory $formFactory)
{
$this->formFactory = $formFactory;
}
public function create()
{
$form = $this->formFactory->create();
$form->addText('foo', 'Foo');
$form->addSubmit('_submit', 'Save');
return $form;
}
}
Define nested formFactory in constructor
services:
fooFormFactory:
class: FooFormFactory(@basicFormFactory)
class ExamplePresenter extends Nette\Application\UI\Presenter
{
private $fooFormFactory;
public function __construct(FooFormFactory $fooFormFactory)
{
$this->fooFormFactory = $fooFormFactory;
}
public function createComponentFooForm()
{
$form = $this->fooFormFactory->create();
$form->onSuccess[] = $this->fooFormSuccess;
return $form;
}
public function fooFormSuccess($form)
{
...
}
}
use Venne\Bridges\Kdyby\DoctrineForms\FormFactoryFactory;
class ExamplePresenter extends Nette\Application\UI\Presenter
{
private $fooFormFactory;
private $formFactoryFactory;
public function __construct(FooFormFactory $fooFormFactory, FormFactoryFactory $formFactoryFactory)
{
$this->fooFormFactory = $fooFormFactory;
$this->formFactoryFactory = $formFactoryFactory;
}
public function createComponentFooForm()
{
$entity = ....;
$form = $this->formFactoryFactory
->create($this->fooFormFactory)
->setEntity($entity)
->create();
$form->onSuccess[] = $this->fooFormSuccess;
return $form;
}
public function fooFormSuccess($form)
{
...
}
}