Last active
November 5, 2022 10:28
-
-
Save miroslav-mrazek/21c242e1e03917fc10dc00f58376e76c to your computer and use it in GitHub Desktop.
Nette form factory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Model\Data; | |
use Nette\Application\UI\Form; | |
class FooFormFactory | |
{ | |
/** @var FormFactory */ | |
protected $formFactory; | |
/** @var FooRepository */ | |
protected $fooRepository; | |
public function __construct(FormFactory $formFactory, FooRepository $fooRepository) | |
{ | |
$this->formFactory = $formFactory; | |
$this->fooRepository = $fooRepository; | |
} | |
/** | |
* @return Form | |
*/ | |
public function create() | |
{ | |
$form = $this->formFactory->create(); | |
$form->addHidden('id'); | |
$form->addText('foo', 'Foo'); | |
$form->addSubmit('ok', "Uložit"); | |
$form->onValidate[] = [$this, 'validate']; | |
$form->onSuccess['save'] = [$this, 'save']; | |
return $form; | |
} | |
public function validate(Form $form, $values) | |
{ | |
} | |
/** | |
* Jen persistence dat. Přesměrování, flashmessage a podobné rutiny musí zařídit komponenta, nebo presenter. | |
*/ | |
public function save(Form $form, $values) | |
{ | |
$id = $values->id; | |
unset($values->id); | |
if (empty($id)) { # nový záznam | |
$foo = new Foo($values); | |
$this->fooRepository->persist($foo); | |
} | |
else { # editace záznamu | |
$foo = $this->fooRepository->get($id); | |
$foo->assign($values); | |
$this->fooRepository->persist($foo); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Presenters; | |
use App\Model\Data\FooRepository; | |
use App\Model\Data\FooFormFactory; | |
class FooPresenter extends BasePresenter | |
{ | |
/** @var FooRepository @inject */ | |
public $fooRepository; | |
/** @var FooFormFactory @inject */ | |
public $fooFormFactory; | |
public function actionNew() | |
{ | |
$this['fooForm']->addSubmit('sendAndNew', "Uložit a zadat další"); | |
} | |
public function actionEdit($id) | |
{ | |
$this['fooForm']->setDefaults( $this->fooRepository->get($id)->toArray() ); | |
} | |
protected function createComponentFooForm() | |
{ | |
$form = $this->fooFormFactory->create(); | |
$form->onSuccess['afterSave'] = | |
function($form, $values) | |
{ | |
if (empty($id)) { # nový záznam | |
$this->flashMessage("Foo bylo vytvořeno.", 'success'); | |
} | |
else { # editace záznamu | |
$this->flashMessage("Foo bylo upraveno.", 'success'); | |
} | |
if ($form->submitted->name === 'sendAndNew') { | |
$this->redirect('new'); | |
} | |
else { | |
$this->redirect('list'); | |
} | |
}; | |
return $form; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Model\Data; | |
use Nette\Application\UI\Form; | |
use Nette\Forms\IFormRenderer; | |
class FormFactory | |
{ | |
/** @var IFormRenderer */ | |
protected $renderer; | |
public function __construct(IFormRenderer $renderer = NULL) | |
{ | |
$this->renderer = $renderer; | |
} | |
/** | |
* @return Form | |
*/ | |
public function create() | |
{ | |
$form = new Form; | |
$form->onSuccess['beforeSave'] = function(){}; # nutné kvůli pořadí callbacků | |
$form->onSuccess['save'] = function(){}; # nutné kvůli pořadí callbacků | |
$form->onSuccess['afterSave'] = [$this, 'afterSave']; | |
$form->setRenderer($this->renderer); | |
return $form; | |
} | |
/** | |
* Jen provizovní přesměrování, tohle by měla řešit komponenta, nebo presenter. | |
*/ | |
public function afterSave(Form $form, $values) | |
{ | |
trigger_error('Ošetřete tento callback v komponentě, nebo presenteru.', E_USER_NOTICE); | |
$form->presenter->redirect('this'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment