|
2 | 2 |
|
3 | 3 | ## Content |
4 | 4 |
|
| 5 | +- [ApplicationFormFactoryExtension - provides Nette\Application\UI\Form factory](#application-form-factory) |
5 | 6 | - [StandaloneFormFactoryExtension - provides Nette\Forms\Form factory](#standalone-form-factory) |
6 | 7 |
|
| 8 | +## Application Form Factory |
| 9 | + |
| 10 | +ApplicationFormFactory returns instance of Nette\Application\UI\Form. It should be used instead of StandaloneFormFactory if [nette/application](https://github.com/nette/application) is installed. |
| 11 | + |
| 12 | +```yaml |
| 13 | +extensions: |
| 14 | + forms.application: Contributte\Forms\DI\ApplicationFormFactoryExtension |
| 15 | +``` |
| 16 | +
|
| 17 | +You can override it by your implementation. |
| 18 | +
|
| 19 | +```yaml |
| 20 | +services: |
| 21 | + forms.application.factory: My\FormFactory |
| 22 | +``` |
| 23 | +
|
| 24 | +Straightforward is to **inject** factory to presenter. |
| 25 | +
|
| 26 | +```php |
| 27 | +namespace App\Presenters; |
| 28 | + |
| 29 | +use Contributte\Forms\IApplicationFormFactory; |
| 30 | +use Nette\Application\UI\Form; |
| 31 | + |
| 32 | +final class UserPresenter extends BasePresenter |
| 33 | +{ |
| 34 | + |
| 35 | + /** @var IApplicationFormFactory @inject */ |
| 36 | + public $factory; |
| 37 | + |
| 38 | + protected function createComponentUserForm(): Form |
| 39 | + { |
| 40 | + $form = $this->factory->create(); |
| 41 | + |
| 42 | + // Add inputs here! |
| 43 | + |
| 44 | + return $form; |
| 45 | + } |
| 46 | + |
| 47 | +} |
| 48 | +``` |
| 49 | + |
| 50 | +Even better is to use factory in your custom form factory. |
| 51 | + |
| 52 | +```php |
| 53 | +namespace App\Forms; |
| 54 | + |
| 55 | +use Contributte\Forms\IApplicationFormFactory; |
| 56 | +use Nette\Application\UI\Form; |
| 57 | + |
| 58 | +final class UserFormFactory |
| 59 | +{ |
| 60 | + |
| 61 | + /** @var IApplicationFormFactory */ |
| 62 | + private $factory; |
| 63 | + |
| 64 | + public function __construct(IApplicationFormFactory $factory) |
| 65 | + { |
| 66 | + $this->factory = $factory; |
| 67 | + } |
| 68 | + |
| 69 | + public function create(): Form |
| 70 | + { |
| 71 | + $form = $this->factory->create(); |
| 72 | + |
| 73 | + // Add inputs here! |
| 74 | + |
| 75 | + return $form; |
| 76 | + } |
| 77 | + |
| 78 | +} |
| 79 | +``` |
| 80 | + |
7 | 81 | ## Standalone Form Factory |
8 | 82 |
|
| 83 | +StandaloneFormFactory returns instance of Nette\Forms\Form. It should be used only if [nette/application](https://github.com/nette/application) is not installed. |
| 84 | + |
9 | 85 | ```yaml |
10 | 86 | extensions: |
11 | 87 | forms.standalone: Contributte\Forms\DI\StandaloneFormFactoryExtension |
|
0 commit comments