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: * Hidden form control used to store a non-displayed value.
15: */
16: class HiddenField extends BaseControl
17: {
18: /** @var bool */
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: * Sets control's value.
37: * @param string
38: * @return static
39: * @internal
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: * Generates control's HTML element.
55: * @return Nette\Utils\Html
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: * Bypasses label generation.
71: * @return void
72: */
73: public function getLabel($caption = NULL)
74: {
75: return NULL;
76: }
77:
78:
79: /**
80: * Adds error message to the list.
81: * @param string error message
82: * @return void
83: */
84: public function addError($message)
85: {
86: $this->getForm()->addError($message);
87: }
88:
89: }
90: