control->type = 'checkbox'; $this->container = Nette\Utils\Html::el(); $this->separator = Nette\Utils\Html::el('br'); if ($items !== NULL) { $this->setItems($items); } } /** * Returns selected radio value. NULL means nothing have been checked. * * @return mixed */ public function getValue() { return is_array($this->value) ? $this->value : NULL; } /** * Sets options from which to choose. * * @param array $items * @return CheckboxList provides a fluent interface */ public function setItems(array $items) { $this->items = $items; return $this; } /** * Returns options from which to choose. * * @return array */ public function getItems() { return $this->items; } /** * Returns separator HTML element template. * * @return Nette\Utils\Html */ public function getSeparatorPrototype() { return $this->separator; } /** * Returns container HTML element template. * * @return Nette\Utils\Html */ public function getContainerPrototype() { return $this->container; } /** * Generates control's HTML element. * * @param mixed $key Specify a key if you want to render just a single checkbox * @return Nette\Utils\Html */ public function getControl($key = NULL) { if ($key === NULL) { $container = clone $this->container; $separator = (string) $this->separator; } elseif (!isset($this->items[$key])) { return NULL; } $control = parent::getControl(); $control->name .= '[]'; $id = $control->id; $counter = -1; $values = $this->value === NULL ? NULL : (array) $this->getValue(); $label = Nette\Utils\Html::el('label'); foreach ($this->items as $k => $val) { $counter++; if ($key !== NULL && $key != $k) continue; // intentionally == $control->id = $label->for = $id . '-' . $counter; $control->checked = (count($values) > 0) ? in_array($k, $values) : false; $control->value = $k; if ($val instanceof Nette\Utils\Html) { $label->setHtml($val); } else { $label->setText($this->translate($val)); } if ($key !== NULL) { return (string) $control . (string) $label; } $container->add((string) $control . (string) $label . $separator); } return $container; } /** * Generates label's HTML element. * * @return Html */ public function getLabel($caption = NULL) { $label = parent::getLabel($caption); $label->for = NULL; return $label; } /** * Filled validator: has been any checkbox checked? * * @param Nette\Forms\IControl $control * @return bool */ public static function validateChecked(Nette\Forms\IControl $control) { return $control->getValue() !== NULL; } }