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