PluginProbe
Packeta / 1.6.0
Packeta v1.6.0
2.3.2 2.3.1 trunk 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.3.0 1.3.1 1.3.2 1.4 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 All 56 releases
packeta / deps / nette / forms / src / Forms / Controls / HiddenField.php

HiddenField.php in Packeta 1.6.0, at deps/nette/forms/src/Forms/Controls/HiddenField.php

92 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 declare (strict_types=1);
8 namespace Packetery\Nette\Forms\Controls;
9
10 use Packetery\Nette;
11 /**
12 * Hidden form control used to store a non-displayed value.
13 */
14 class HiddenField extends BaseControl
15 {
16 /** @var bool */
17 private $persistValue;
18 /** @var bool */
19 private $nullable = \false;
20 public function __construct($persistentValue = null)
21 {
22 parent::__construct();
23 $this->control->type = 'hidden';
24 $this->setOption('type', 'hidden');
25 if ($persistentValue !== null) {
26 $this->unmonitor(\Packetery\Nette\Forms\Form::class);
27 $this->persistValue = \true;
28 $this->value = (string) $persistentValue;
29 }
30 }
31 /**
32 * Sets control's value.
33 * @return static
34 * @internal
35 */
36 public function setValue($value)
37 {
38 if ($value === null) {
39 $value = '';
40 } elseif (!\is_scalar($value) && !(\is_object($value) && \method_exists($value, '__toString'))) {
41 throw new \Packetery\Nette\InvalidArgumentException(\sprintf("Value must be scalar or null, %s given in field '%s'.", \gettype($value), $this->name));
42 }
43 if (!$this->persistValue) {
44 $this->value = $value;
45 }
46 return $this;
47 }
48 public function getValue()
49 {
50 return $this->nullable && $this->value === '' ? null : $this->value;
51 }
52 /**
53 * Sets whether getValue() returns null instead of empty string.
54 * @return static
55 */
56 public function setNullable(bool $value = \true)
57 {
58 $this->nullable = $value;
59 return $this;
60 }
61 /**
62 * Appends input string filter callback.
63 * @return static
64 */
65 public function addFilter(callable $filter)
66 {
67 $this->getRules()->addFilter($filter);
68 return $this;
69 }
70 public function getControl() : \Packetery\Nette\Utils\Html
71 {
72 $this->setOption('rendered', \true);
73 $el = clone $this->control;
74 return $el->addAttributes(['name' => $this->getHtmlName(), 'disabled' => $this->isDisabled(), 'value' => (string) $this->value]);
75 }
76 /**
77 * Bypasses label generation.
78 */
79 public function getLabel($caption = null)
80 {
81 return null;
82 }
83 /**
84 * Adds error message to the list.
85 * @param string|object $message
86 */
87 public function addError($message, bool $translate = \true) : void
88 {
89 $this->getForm()->addError($message, $translate);
90 }
91 }
92