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: if ($persistentValue !== NULL) {
27: $this->unmonitor('Nette\Forms\Form');
28: $this->persistValue = TRUE;
29: $this->value = (string) $persistentValue;
30: }
31: }
32:
33:
34: /**
35: * Sets control's value.
36: * @param string
37: * @return static
38: * @internal
39: */
40: public function setValue($value)
41: {
42: if (!is_scalar($value) && $value !== NULL && !method_exists($value, '__toString')) {
43: throw new Nette\InvalidArgumentException(sprintf("Value must be scalar or NULL, %s given in field '%s'.", gettype($value), $this->name));
44: }
45: if (!$this->persistValue) {
46: $this->value = (string) $value;
47: }
48: return $this;
49: }
50:
51:
52: /**
53: * Generates control's HTML element.
54: * @return Nette\Utils\Html
55: */
56: public function getControl()
57: {
58: $this->setOption('rendered', TRUE);
59: $el = clone $this->control;
60: return $el->addAttributes(array(
61: 'name' => $this->getHtmlName(),
62: 'disabled' => $this->isDisabled(),
63: 'value' => $this->value,
64: ));
65: }
66:
67:
68: /**
69: * Bypasses label generation.
70: * @return void
71: */
72: public function getLabel($caption = NULL)
73: {
74: return NULL;
75: }
76:
77:
78: /**
79: * Adds error message to the list.
80: * @param string error message
81: * @return void
82: */
83: public function addError($message)
84: {
85: $this->getForm()->addError($message);
86: }
87:
88: }
89: