1: <?php
2:
3: 4: 5: 6:
7:
8: namespace Nette\Forms\Controls;
9:
10: use Nette;
11:
12:
13: 14: 15:
16: class HiddenField extends BaseControl
17: {
18:
19: private $persistValue;
20:
21:
22: public function __construct($persistentValue = null)
23: {
24: parent::__construct();
25: $this->control->type = 'hidden';
26: $this->setOption('type', 'hidden');
27: if ($persistentValue !== null) {
28: $this->unmonitor(Nette\Forms\Form::class);
29: $this->persistValue = true;
30: $this->value = (string) $persistentValue;
31: }
32: }
33:
34:
35: 36: 37: 38: 39: 40:
41: public function setValue($value)
42: {
43: if (!is_scalar($value) && $value !== null && !method_exists($value, '__toString')) {
44: throw new Nette\InvalidArgumentException(sprintf("Value must be scalar or null, %s given in field '%s'.", gettype($value), $this->name));
45: }
46: if (!$this->persistValue) {
47: $this->value = (string) $value;
48: }
49: return $this;
50: }
51:
52:
53: 54: 55: 56:
57: public function getControl()
58: {
59: $this->setOption('rendered', true);
60: $el = clone $this->control;
61: return $el->addAttributes([
62: 'name' => $this->getHtmlName(),
63: 'disabled' => $this->isDisabled(),
64: 'value' => $this->value,
65: ]);
66: }
67:
68:
69: 70: 71: 72: 73:
74: public function getLabel($caption = null)
75: {
76: }
77:
78:
79: 80: 81: 82: 83:
84: public function addError($message, $translate = true)
85: {
86: $this->getForm()->addError($message, $translate);
87: }
88: }
89: