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: * @author David Grudl
17: */
18: class HiddenField extends BaseControl
19: {
20: /** @var bool */
21: private $persistValue;
22:
23:
24: public function __construct($persistentValue = NULL)
25: {
26: parent::__construct();
27: $this->control->type = 'hidden';
28: if ($persistentValue !== NULL) {
29: $this->unmonitor('Nette\Forms\Form');
30: $this->persistValue = TRUE;
31: $this->value = (string) $persistentValue;
32: }
33: }
34:
35:
36: /**
37: * Sets control's value.
38: * @param string
39: * @return self
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(array(
62: 'name' => $this->getHtmlName(),
63: 'id' => $this->getHtmlId(),
64: 'disabled' => $this->isDisabled(),
65: 'value' => $this->value,
66: ));
67: }
68:
69:
70: /**
71: * Bypasses label generation.
72: * @return void
73: */
74: public function getLabel($caption = NULL)
75: {
76: return NULL;
77: }
78:
79:
80: /**
81: * Adds error message to the list.
82: * @param string error message
83: * @return void
84: */
85: public function addError($message)
86: {
87: $this->getForm()->addError($message);
88: }
89:
90: }
91: