1: <?php
2:
3: /**
4: * This file is part of the Nette Framework (https://nette.org)
5: * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
6: */
7:
8: namespace Nette\Forms\Controls;
9:
10: use Nette;
11:
12:
13: /**
14: * Check box control. Allows the user to select a true or false condition.
15: */
16: class Checkbox extends BaseControl
17: {
18: /** @var Nette\Utils\Html wrapper element template */
19: private $wrapper;
20:
21:
22: /**
23: * @param string label
24: */
25: public function __construct($label = NULL)
26: {
27: parent::__construct($label);
28: $this->control->type = 'checkbox';
29: $this->wrapper = Nette\Utils\Html::el();
30: }
31:
32:
33: /**
34: * Sets control's value.
35: * @param bool
36: * @return static
37: * @internal
38: */
39: public function setValue($value)
40: {
41: if (!is_scalar($value) && $value !== NULL) {
42: throw new Nette\InvalidArgumentException(sprintf("Value must be scalar or NULL, %s given in field '%s'.", gettype($value), $this->name));
43: }
44: $this->value = (bool) $value;
45: return $this;
46: }
47:
48:
49: /**
50: * Is control filled?
51: * @return bool
52: */
53: public function isFilled()
54: {
55: return $this->getValue() !== FALSE; // back compatibility
56: }
57:
58:
59: /**
60: * Generates control's HTML element.
61: * @return Nette\Utils\Html
62: */
63: public function getControl()
64: {
65: return $this->wrapper->setHtml($this->getLabelPart()->insert(0, $this->getControlPart()));
66: }
67:
68:
69: /**
70: * Bypasses label generation.
71: * @return void
72: */
73: public function getLabel($caption = NULL)
74: {
75: return NULL;
76: }
77:
78:
79: /**
80: * @return Nette\Utils\Html
81: */
82: public function getControlPart()
83: {
84: return parent::getControl()->checked($this->value);
85: }
86:
87:
88: /**
89: * @return Nette\Utils\Html
90: */
91: public function getLabelPart()
92: {
93: return parent::getLabel();
94: }
95:
96:
97: /**
98: * Returns wrapper HTML element template.
99: * @return Nette\Utils\Html
100: */
101: public function getSeparatorPrototype()
102: {
103: return $this->wrapper;
104: }
105:
106: }
107: