-
-
Save fprochazka/943164 to your computer and use it in GitHub Desktop.
Replicator ... $form->addDynamic() ... Více na https://github.com/HosipLan/Nette-addDynamic
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 | |
/** | |
* @see https://github.com/HosipLan/Nette-addDynamic | |
*/ | |
class CreatePresenter extends BasePresenter | |
{ | |
protected function startup() | |
{ | |
parent::startup(); | |
// optionaly: Kdyby\Forms\Containers\Replicator::register('addReplicator'); | |
Kdyby\Forms\Containers\Replicator::register(); | |
} | |
/** | |
* @return Nette\Application\UI\Form | |
*/ | |
protected function createComponentMyForm() | |
{ | |
$form = new Nette\Application\UI\Form; | |
// jméno, továrnička, výchozí počet | |
$replicator = $form->addDynamic('users', function (Nette\Forms\Container $container) { | |
$container->currentGroup = $container->getForm()->addGroup('skupina', FALSE); | |
$container->addText('name', 'Jméno'); | |
$container->addText('surname', 'Příjmení'); | |
}, 2); | |
$replicator->addSubmit('add', 'Přidat dalšího člověka') | |
->setValidationScope(FALSE) | |
->onClick[] = callback($this, 'MyFormAddElementClicked'); | |
$form->addGroup(); | |
$form->addSubmit('send', 'Zpracovat') | |
->onclick[] = callback($this, 'MyFormSubmitted'); | |
return $form; | |
} | |
/** | |
* @param Nette\Forms\Controls\SubmitButton $button | |
*/ | |
public function MyFormAddElementClicked(Nette\Forms\Controls\SubmitButton $button) | |
{ | |
$users = $button->form['users']; | |
// přidá jeden řádek do containeru | |
// počítá, že identifikátory budou čísla | |
$users[$users->countFilledWithout(array('add'))]; //touch | |
} | |
/** | |
* @param Nette\Forms\Controls\SubmitButton $button | |
*/ | |
public function MyFormSubmitted(Nette\Forms\Controls\SubmitButton $button) | |
{ | |
foreach ($button->form->values['users'] as $user) { | |
dump($user['name'], $user['surname']); | |
} | |
} | |
} |
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 | |
/** | |
* @see https://github.com/HosipLan/Nette-addDynamic | |
*/ | |
class EditPresenter extends BasePresenter | |
{ | |
private $ukolModel; | |
private $ukol; | |
private $ukolUsers = array(); | |
protected function startup() | |
{ | |
parent::startup(); | |
// optionaly: Kdyby\Forms\Containers\Replicator::register('addReplicator'); | |
Kdyby\Forms\Containers\Replicator::register(); | |
$this->ukolModel = new UkolModel($this->context->db); // nebo přímo z containeru | |
} | |
public function actionDefault($id) | |
{ | |
$this->ukolUsers = $ukolUsers = $this->ukolModel->findUsersById($id); | |
$this->ukol = $this->ukolModel->find($id); | |
$this['form']->setDefaultValues($this->ukol); | |
foreach ($this['form']['users']->containers as $user) { | |
$user->setDefaults(array_shift($ukolUsers)); | |
} | |
} | |
/** | |
* @return Nette\Application\UI\Form | |
*/ | |
protected function createComponentMyForm() | |
{ | |
$form = new Nette\Application\UI\Form; | |
// jméno, továrnička, výchozí počet | |
$replicator = $form->addDynamic('users', function (Nette\Forms\Container $container) { | |
$container->currentGroup = $container->getForm()->addGroup('skupina', FALSE); | |
$container->addText('name', 'Jméno'); | |
$container->addText('surname', 'Příjmení'); | |
}, count($this->ukolUsers)); | |
$replicator->addSubmit('add', 'Přidat dalšího člověka') | |
->setValidationScope(FALSE) | |
->onClick[] = callback($this, 'MyFormAddElementClicked'); | |
$form->addGroup(); | |
$form->addSubmit('send', 'Zpracovat') | |
->onclick[] = callback($this, 'MyFormSubmitted'); | |
return $form; | |
} | |
/** | |
* @param Nette\Forms\Controls\SubmitButton $button | |
*/ | |
public function MyFormAddElementClicked(Nette\Forms\Controls\SubmitButton $button) | |
{ | |
$users = $button->form['users']; | |
// přidá jeden řádek do containeru | |
// počítá, že identifikátory budou čísla | |
$users[$users->countFilledWithout(array('add'))]; //touch | |
} | |
/** | |
* @param Nette\Forms\Controls\SubmitButton $button | |
*/ | |
public function MyFormSubmitted(Nette\Forms\Controls\SubmitButton $button) | |
{ | |
foreach ($button->form->values['users'] as $user) { | |
dump($user['name'], $user['surname']); | |
} | |
} | |
} |
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
see: https://github.com/Kdyby/ReplicatorExtension | |
sandbox: https://github.com/HosipLan/Nette-addDynamic |
Nepodarilo se vyresit toto: http://forum.nette.org/cs/7286-pridavani-polozek-za-behu-aplikace-dynamicke-formulare#p57097 ?
Ano, trošku nerad, ale dodělal jsem podporu pro smazání Containeru a kompletní zametení stop po něm, pomocí reflexe.
https://github.com/Kdyby/Framework/blob/master/libs/Kdyby/Forms/Containers/Replicator.php#L174
foreach ($this['form']['users'] as $user)
v ukázce EditPresenteru mi nefunguje, funguje foreach ($this['form']['users']->containers as $user)
Ano, máš pravdu, je tam chyba. Díky opravím. Koukni raději na sandbox http://github.com/hosiplan/nette-adddynamic
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Zrušení výpisu chyb bychpořešil asi takto nějak: