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