Skip to content

Instantly share code, notes, and snippets.

@fprochazka
Forked from JanTvrdik/ReplicatingContainer.php
Created April 26, 2011 21:14
Show Gist options
  • Save fprochazka/943164 to your computer and use it in GitHub Desktop.
Save fprochazka/943164 to your computer and use it in GitHub Desktop.
Replicator ... $form->addDynamic() ... Více na https://github.com/HosipLan/Nette-addDynamic
<?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']);
}
}
}
<?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']);
}
}
}
see: https://github.com/Kdyby/ReplicatorExtension
sandbox: https://github.com/HosipLan/Nette-addDynamic
@michalsvec
Copy link

@fprochazka
Copy link
Author

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

Ukázka: https://github.com/HosipLan/Nette-addDynamic/blob/master/app/presenters/HomepagePresenter.php#L82

@karzac
Copy link

karzac commented Aug 24, 2011

foreach ($this['form']['users'] as $user) v ukázce EditPresenteru mi nefunguje, funguje foreach ($this['form']['users']->containers as $user)

@fprochazka
Copy link
Author

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