-
-
Save iguana007/945971 to your computer and use it in GitHub Desktop.
ReplicatingContainer
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 | |
class NejakyPresenter 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->touch($users->countFilledWithout(array('add'))); | |
} | |
/** | |
* @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/Framework/blob/master/Kdyby/Forms/Containers/Replicator.php |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment