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