Skip to content

Commit

Permalink
Merge pull request #11 from chapcz/master
Browse files Browse the repository at this point in the history
Added multiple values
  • Loading branch information
duskohu committed Dec 1, 2016
2 parents e5a9574 + 89f8599 commit 5e48656
Showing 1 changed file with 64 additions and 4 deletions.
68 changes: 64 additions & 4 deletions src/NasExt/Forms/Controls/DependentSelectBox.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Nette\Forms\Controls\SelectBox;
use Nette\Forms\Container;
use Nette\InvalidStateException;
use Nette\InvalidArgumentException;
use Nette\Utils\Callback;
use Nette\Utils\Html;
use Nette\Utils\Json;
Expand All @@ -42,13 +43,13 @@ class DependentSelectBox extends SelectBox implements ISignalReceiver

/** @var bool */
private $disabledWhenEmpty;

/** @var bool */
protected $disabled;

/** @var mixed */
private $tempValue;

/** @var bool */
private $multiple;

Expand Down Expand Up @@ -122,7 +123,7 @@ public function setDisabled($value = TRUE)
{
$this->disabled = $value;
return $this;
}
}

/**
* Returns selected key.
Expand All @@ -131,6 +132,9 @@ public function setDisabled($value = TRUE)
public function getValue()
{
$this->tryLoadItems();
if ($this->multiple) {
return array_values(array_intersect($this->value, array_keys($this->items)));
}
return parent::getValue();
}

Expand All @@ -156,7 +160,11 @@ public function setItems(array $items, $useKeys = TRUE)
{
parent::setItems($items, $useKeys);
if ($this->tempValue != NULL) {
parent::setValue($this->tempValue);
if ($this->multiple){
$this->setMultipleValue($this->tempValue);
} else {
parent::setValue($this->tempValue);
}
}
}

Expand Down Expand Up @@ -314,4 +322,56 @@ public static function addDependentSelectBox(Container $container, $name, $label
$container[$name] = new self($label, $parents, $dependentCallback, $multiple);
return $container[$name];
}

/**
* Returns HTML name of control.
* @return string
*/
public function getHtmlName()
{
return parent::getHtmlName() . ($this->multiple ? '[]' : '');
}


public function loadHttpData()
{
if (!$this->multiple){
parent::loadHttpData();
return;
}
$this->value = array_keys(array_flip($this->getHttpData(\Nette\Forms\Form::DATA_TEXT)));
if (is_array($this->disabled)) {
$this->value = array_diff($this->value, array_keys($this->disabled));
}
}

/**
* Sets selected items (by keys).
* @param array
* @return self
* @internal
*/
private function setMultipleValue($values)
{
if (is_scalar($values) || $values === NULL) {
$values = (array) $values;
} elseif (!is_array($values)) {
throw new Nette\InvalidArgumentException(sprintf("Value must be array or NULL, %s given in field '%s'.", gettype($values), $this->name));
}
$flip = array();
foreach ($values as $value) {
if (!is_scalar($value) && !method_exists($value, '__toString')) {
throw new Nette\InvalidArgumentException(sprintf("Values must be scalar, %s given in field '%s'.", gettype($value), $this->name));
}
$flip[(string) $value] = TRUE;
}
$values = array_keys($flip);
if ($this->checkAllowedValues && ($diff = array_diff($values, array_keys($this->items)))) {
$set = Nette\Utils\Strings::truncate(implode(', ', array_map(function ($s) { return var_export($s, TRUE); }, array_keys($this->items))), 70, '...');
$vals = (count($diff) > 1 ? 's' : '') . " '" . implode("', '", $diff) . "'";
throw new Nette\InvalidArgumentException("Value$vals are out of allowed set [$set] in field '{$this->name}'.");
}
$this->value = $values;
return $this;
}
}

0 comments on commit 5e48656

Please sign in to comment.