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: * @author David Grudl
17: */
18: class Checkbox extends BaseControl
19: {
20: /** @var Nette\Utils\Html wrapper element template */
21: private $wrapper;
22:
23:
24: /**
25: * @param string label
26: */
27: public function __construct($label = NULL)
28: {
29: parent::__construct($label);
30: $this->control->type = 'checkbox';
31: $this->wrapper = Nette\Utils\Html::el();
32: }
33:
34:
35: /**
36: * Sets control's value.
37: * @param bool
38: * @return self
39: */
40: public function setValue($value)
41: {
42: if (!is_scalar($value) && $value !== NULL) {
43: throw new Nette\InvalidArgumentException(sprintf("Value must be scalar or NULL, %s given in field '%s'.", gettype($value), $this->name));
44: }
45: $this->value = (bool) $value;
46: return $this;
47: }
48:
49:
50: /**
51: * Is control filled?
52: * @return bool
53: */
54: public function isFilled()
55: {
56: return $this->getValue() !== FALSE; // back compatibility
57: }
58:
59:
60: /**
61: * Generates control's HTML element.
62: * @return Nette\Utils\Html
63: */
64: public function getControl()
65: {
66: return $this->wrapper->setHtml($this->getLabelPart()->insert(0, $this->getControlPart()));
67: }
68:
69:
70: /**
71: * Bypasses label generation.
72: * @return void
73: */
74: public function getLabel($caption = NULL)
75: {
76: return NULL;
77: }
78:
79:
80: /**
81: * @return Nette\Utils\Html
82: */
83: public function getControlPart()
84: {
85: return parent::getControl()->checked($this->value);
86: }
87:
88:
89: /**
90: * @return Nette\Utils\Html
91: */
92: public function getLabelPart()
93: {
94: return parent::getLabel();
95: }
96:
97:
98: /**
99: * Returns wrapper HTML element template.
100: * @return Nette\Utils\Html
101: */
102: public function getSeparatorPrototype()
103: {
104: return $this->wrapper;
105: }
106:
107: }
108: