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: * @param string
33: * @return self
34: * @internal
35: */
36: public function setValue($value)
37: {
38: if ($value === NULL) {
39: $value = '';
40: } elseif (!is_scalar($value) && !method_exists($value, '__toString')) {
41: throw new Nette\InvalidArgumentException(sprintf("Value must be scalar or NULL, %s given in field '%s'.", gettype($value), $this->name));
42: }
43: $this->rawValue = $this->value = $value;
44: return $this;
45: }
46:
47:
48: /**
49: * Returns control's value.
50: * @return string
51: */
52: public function getValue()
53: {
54: return $this->nullable && $this->value === '' ? NULL : $this->value;
55: }
56:
57:
58: /**
59: * Sets whether getValue() returns NULL instead of empty string.
60: * @param bool
61: * @return self
62: */
63: public function setNullable($value = TRUE)
64: {
65: $this->nullable = (bool) $value;
66: return $this;
67: }
68:
69:
70: /**
71: * Sets the special value which is treated as empty string.
72: * @param string
73: * @return self
74: */
75: public function setEmptyValue($value)
76: {
77: $this->emptyValue = (string) $value;
78: return $this;
79: }
80:
81:
82: /**
83: * Returns the special value which is treated as empty string.
84: * @return string
85: */
86: public function getEmptyValue()
87: {
88: return $this->emptyValue;
89: }
90:
91:
92: /**
93: * Sets the maximum number of allowed characters.
94: * @param int
95: * @return self
96: */
97: public function setMaxLength($length)
98: {
99: $this->control->maxlength = $length;
100: return $this;
101: }
102:
103:
104: /**
105: * Appends input string filter callback.
106: * @param callable
107: * @return self
108: */
109: public function addFilter($filter)
110: {
111: $this->getRules()->addFilter($filter);
112: return $this;
113: }
114:
115:
116: public function getControl()
117: {
118: $el = parent::getControl();
119: if ($this->emptyValue !== '') {
120: $el->attrs['data-nette-empty-value'] = Strings::trim($this->translate($this->emptyValue));
121: }
122: if (isset($el->placeholder)) {
123: $el->placeholder = $this->translate($el->placeholder);
124: }
125: return $el;
126: }
127:
128:
129: /**
130: * @return string|NULL
131: */
132: protected function getRenderedValue()
133: {
134: return $this->rawValue === ''
135: ? ($this->emptyValue === '' ? NULL : $this->translate($this->emptyValue))
136: : $this->rawValue;
137: }
138:
139:
140: public function addRule($validator, $message = NULL, $arg = NULL)
141: {
142: if ($validator === Form::LENGTH || $validator === Form::MAX_LENGTH) {
143: $tmp = is_array($arg) ? $arg[1] : $arg;
144: if (is_scalar($tmp)) {
145: $this->control->maxlength = isset($this->control->maxlength) ? min($this->control->maxlength, $tmp) : $tmp;
146: }
147: }
148: return parent::addRule($validator, $message, $arg);
149: }
150:
151:
152: /**
153: * Performs the server side validation.
154: * @return void
155: */
156: public function validate()
157: {
158: if ($this->value === Strings::trim($this->translate($this->emptyValue))) {
159: $this->value = '';
160: }
161: parent::validate();
162: }
163:
164: }
165: