-
-
Notifications
You must be signed in to change notification settings - Fork 147
Expand file tree
/
Copy pathTextBase.php
More file actions
148 lines (120 loc) · 3.03 KB
/
TextBase.php
File metadata and controls
148 lines (120 loc) · 3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
/**
* This file is part of the Nette Framework (https://nette.org)
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
*/
declare(strict_types=1);
namespace Nette\Forms\Controls;
use Nette;
use Nette\Forms\Form;
use Nette\Utils\Strings;
/**
* Implements the basic functionality common to text input controls.
*/
abstract class TextBase extends BaseControl
{
/** @var string */
protected $emptyValue = '';
/** @var mixed unfiltered submitted value */
protected $rawValue = '';
/** @var bool */
private $nullable;
/**
* Sets control's value.
* @return static
* @internal
*/
public function setCurrentValue($value)
{
if ($value === null) {
$value = '';
} elseif (!is_scalar($value) && !method_exists($value, '__toString')) {
throw new Nette\InvalidArgumentException(sprintf("Value must be scalar or null, %s given in field '%s'.", gettype($value), $this->name));
}
$this->value = $value;
$this->rawValue = (string) $value;
return $this;
}
/**
* Returns control's value.
* @return mixed
*/
public function getValue()
{
$value = $this->value === Strings::trim($this->translate($this->emptyValue)) ? '' : $this->value;
return $this->nullable && $value === '' ? null : $value;
}
/**
* Sets whether getValue() returns null instead of empty string.
* @return static
*/
public function setNullable(bool $value = true)
{
$this->nullable = $value;
return $this;
}
/**
* Sets the special value which is treated as empty string.
* @return static
*/
public function setEmptyValue(string $value)
{
$this->emptyValue = $value;
return $this;
}
/**
* Returns the special value which is treated as empty string.
*/
public function getEmptyValue(): string
{
return $this->emptyValue;
}
/**
* Sets the maximum number of allowed characters.
* @return static
*/
public function setMaxLength(int $length)
{
$this->control->maxlength = $length;
return $this;
}
/**
* Appends input string filter callback.
* @return static
*/
public function addFilter(callable $filter)
{
$this->getRules()->addFilter($filter);
return $this;
}
public function getControl(): Nette\Utils\Html
{
$el = parent::getControl();
if ($this->emptyValue !== '') {
$el->attrs['data-nette-empty-value'] = Strings::trim($this->translate($this->emptyValue));
}
if (isset($el->placeholder)) {
$el->placeholder = $this->translate($el->placeholder);
}
return $el;
}
protected function getRenderedValue(): ?string
{
return $this->rawValue === ''
? ($this->emptyValue === '' ? null : $this->translate($this->emptyValue))
: $this->rawValue;
}
/**
* @return static
*/
public function addRule($validator, $errorMessage = null, $arg = null)
{
if ($validator === Form::LENGTH || $validator === Form::MAX_LENGTH) {
$tmp = is_array($arg) ? $arg[1] : $arg;
if (is_scalar($tmp)) {
$this->control->maxlength = isset($this->control->maxlength) ? min($this->control->maxlength, $tmp) : $tmp;
}
}
return parent::addRule($validator, $errorMessage, $arg);
}
}