Skip to content

Commit

Permalink
Merge pull request #29 from peldax/patch-1
Browse files Browse the repository at this point in the history
Nette 3.0 compatibility + multiselect fix
  • Loading branch information
duskohu authored May 22, 2019
2 parents 1e11dfc + 616a723 commit b5e5d61
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
21 changes: 19 additions & 2 deletions src/Controls/DependentMultiSelectBox.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
*/
class DependentMultiSelectBox extends Nette\Forms\Controls\MultiSelectBox implements Nette\Application\UI\ISignalReceiver
{
use NasExt\Forms\DependentTrait;
use NasExt\Forms\DependentTrait {
getValue as protected traitGetValue;
}

/** @var string */
const SIGNAL_NAME = DependentSelectBox::SIGNAL_NAME;
Expand Down Expand Up @@ -56,17 +58,32 @@ public function setDisabled($value = true)


/**
* @return array
*/
public function getValue(): array
{
return $this->traitGetValue();
}


/**
* @param string $signal
* @return void
*/
public function signalReceived($signal)
public function signalReceived(string $signal) : void
{
$presenter = $this->lookup('Nette\\Application\\UI\\Presenter');

if ($presenter->isAjax() && $signal === self::SIGNAL_NAME && !$this->isDisabled()) {
$parentsNames = [];
foreach ($this->parents as $parent) {
$value = $presenter->getParameter($this->getNormalizeName($parent));

if ($parent instanceof Nette\Forms\Controls\MultiChoiceControl) {
$value = explode(',', $value);
$value = array_filter($value, static function ($val) {return !in_array($val, [null, '', []], true);});
}

$parent->setValue($value);

$parentsNames[$parent->getName()] = $parent->getValue();
Expand Down
8 changes: 7 additions & 1 deletion src/Controls/DependentSelectBox.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,20 @@ public function __construct($label, array $parents)
* @param string $signal
* @return void
*/
public function signalReceived($signal)
public function signalReceived(string $signal) : void
{
$presenter = $this->lookup('Nette\\Application\\UI\\Presenter');

if ($presenter->isAjax() && $signal === self::SIGNAL_NAME && !$this->isDisabled()) {
$parentsNames = [];
foreach ($this->parents as $parent) {
$value = $presenter->getParameter($this->getNormalizeName($parent));

if ($parent instanceof Nette\Forms\Controls\MultiChoiceControl) {
$value = explode(',', $value);
$value = array_filter($value, static function ($val) {return !in_array($val, [null, '', []], true);});
}

$parent->setValue($value);

$parentsNames[$parent->getName()] = $parent->getValue();
Expand Down
4 changes: 2 additions & 2 deletions src/DependentTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ trait DependentTrait
/**
* @return Nette\Utils\Html
*/
public function getControl()
public function getControl() : Nette\Utils\Html
{
$this->tryLoadItems();

Expand Down Expand Up @@ -112,7 +112,7 @@ private function getDependentData(array $args = [])
throw new NasExt\Forms\DependentCallbackException('Dependent callback for "' . $this->getHtmlId() . '" must be set!');
}

$dependentData = Nette\Utils\Callback::invokeArgs($this->dependentCallback, $args);
$dependentData = call_user_func_array($this->dependentCallback, $args);

if (!($dependentData instanceof NasExt\Forms\DependentData) && !($dependentData instanceof NasExt\Forms\Controls\DependentSelectBoxData)) {
throw new NasExt\Forms\DependentCallbackException('Callback for "' . $this->getHtmlId() . '" must return NasExt\\Forms\\DependentData instance!');
Expand Down

0 comments on commit b5e5d61

Please sign in to comment.