Skip to content

Instantly share code, notes, and snippets.

@foowie
Created December 4, 2010 15:36
Show Gist options
  • Save foowie/728259 to your computer and use it in GitHub Desktop.
Save foowie/728259 to your computer and use it in GitHub Desktop.
DependentSelectBox and DynamicContainer presenter example
{snippet formSnippet}
{widget form}
{/snippet}
<?php
use Nette\Application\Presenter;
use Nette\Application\AppForm;
use Nette\Forms\Form;
use DependentSelectBox\DependentSelectBox;
use DependentSelectBox\JsonDependentSelectBox;
use Nette\Forms\FormContainer;
class ExamplePresenter extends Presenter {
public function actionDefault() {
\Nette\Forms\FormContainer::extensionMethod("addDependentSelectBox", "DependentSelectBox\DependentSelectBox::formAddDependentSelectBox");
\Nette\Forms\FormContainer::extensionMethod("addJsonDependentSelectBox", "DependentSelectBox\JsonDependentSelectBox::formAddJsonDependentSelectBox");
Addons\Forms\DynamicContainer::register();
}
public function renderDefault() {
$this["form"]["dynamicContainer"]->beforeRender();
}
public function getValuesSelect2($form, $dsb) {
$container = $dsb->lookup("Nette\Forms\FormContainer");
return array(
"A" => $container["select1"]->getValue()." - A",
"B" => $container["select1"]->getValue()." - B",
"C" => $container["select1"]->getValue()." - C",
);
}
public function getValuesSelect3($form, $dsb) {
$container = $dsb->lookup("Nette\Forms\FormContainer");
return array(
"a" => $container["select1"]->getValue()." - ".$container["select2"]->getValue()." - a",
"b" => $container["select1"]->getValue()." - ".$container["select2"]->getValue()." - b",
"c" => $container["select1"]->getValue()." - ".$container["select2"]->getValue()." - c",
);
}
public function createComponentForm($name) {
$form = new AppForm($this, $name);
$presenter = $this;
$dynamicContainer = $form->addDynamicContainer("dynamicContainer");
$dynamicContainer->setAddButton(true, "Give me new row !", "addButtonOfDynamicContainer");
$dynamicContainer->setDeleteButton(true, "I dont wana this !", "removeButtonOfDynamicContainer");
$dynamicContainer->setMinCount(1);
$dynamicContainer->setMaxCount(5);
$dynamicContainer->setDefaultCount(2);
$dynamicContainer->setFactory(function($container) use($presenter) {
$container->addSelect("select1", "Výběr 1", array("/"=>"Value = /", "|"=>"Value = |", "\\"=>"Value = \\"));
$container->addDependentSelectBox("select2", "Výběr 2", $container["select1"], callback($presenter, "getValuesSelect2"));
if($presenter->isAjax())
$container["select2"]->addOnSubmitCallback(callback($presenter, "invalidateControl"), "formSnippet");
$container->addDependentSelectBox("select3", "Výběr 3", $container["select2"], callback($presenter, "getValuesSelect3"))
->addRule(Form::FILLED, "'%label' musí být vyplněno !");
if($presenter->isAjax())
$container["select3"]->addOnSubmitCallback(callback($presenter, "invalidateControl"), "formSnippet");
});
$form->addSubmit("final_submit", "Hodnoty !")
->onClick[] = callback($this, "submitForm");
return $form;
}
public function submitForm($button) {
dump($button->getForm()->getValues());
die();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment