1: <?php
2:
3: 4: 5: 6:
7:
8: namespace Nette\Forms\Controls;
9:
10: use Nette,
11: Nette\Forms\Form,
12: Nette\Utils\Strings,
13: Nette\Utils\Validators;
14:
15:
16: 17: 18: 19: 20: 21: 22:
23: abstract class TextBase extends BaseControl
24: {
25:
26: protected $emptyValue = '';
27:
28:
29: protected $filters = array();
30:
31:
32: protected $rawValue = '';
33:
34:
35: 36: 37: 38: 39:
40: public function setValue($value)
41: {
42: if ($value === NULL) {
43: $value = '';
44: } elseif (!is_scalar($value) && !method_exists($value, '__toString')) {
45: throw new Nette\InvalidArgumentException(sprintf("Value must be scalar or NULL, %s given in field '%s'.", gettype($value), $this->name));
46: }
47: $this->rawValue = $this->value = $value;
48: return $this;
49: }
50:
51:
52: 53: 54: 55:
56: public function getValue()
57: {
58: $value = $this->value;
59: if (!empty($this->control->maxlength)) {
60: $value = Nette\Utils\Strings::substring($value, 0, $this->control->maxlength);
61: }
62: foreach ($this->filters as $filter) {
63: $value = (string) call_user_func($filter, $value);
64: }
65: return $value === $this->translate($this->emptyValue) ? '' : $value;
66: }
67:
68:
69: 70: 71: 72: 73:
74: public function setEmptyValue($value)
75: {
76: $this->emptyValue = (string) $value;
77: return $this;
78: }
79:
80:
81: 82: 83: 84:
85: public function getEmptyValue()
86: {
87: return $this->emptyValue;
88: }
89:
90:
91: 92: 93: 94: 95:
96: public function addFilter($filter)
97: {
98: $this->filters[] = Nette\Utils\Callback::check($filter);
99: return $this;
100: }
101:
102:
103: public function getControl()
104: {
105: $el = parent::getControl();
106: if ($this->emptyValue !== '') {
107: $el->attrs['data-nette-empty-value'] = $this->translate($this->emptyValue);
108: }
109: if (isset($el->placeholder)) {
110: $el->placeholder = $this->translate($el->placeholder);
111: }
112: return $el;
113: }
114:
115:
116: public function addRule($validator, $message = NULL, $arg = NULL)
117: {
118: if ($validator === Form::LENGTH || $validator === Form::MAX_LENGTH) {
119: $tmp = is_array($arg) ? $arg[1] : $arg;
120: if (is_scalar($tmp)) {
121: $this->control->maxlength = isset($this->control->maxlength) ? min($this->control->maxlength, $tmp) : $tmp;
122: }
123: }
124: return parent::addRule($validator, $message, $arg);
125: }
126:
127:
128:
129:
130:
131: 132: 133: 134:
135: public static function validateEmail(TextBase $control)
136: {
137: return Validators::isEmail($control->getValue());
138: }
139:
140:
141: 142: 143: 144:
145: public static function validateUrl(TextBase $control)
146: {
147: if (Validators::isUrl($value = $control->getValue())) {
148: return TRUE;
149:
150: } elseif (Validators::isUrl($value = "http://$value")) {
151: $control->setValue($value);
152: return TRUE;
153: }
154: return FALSE;
155: }
156:
157:
158:
159: public static function validateRegexp(TextBase $control, $regexp)
160: {
161: trigger_error('Validator REGEXP is deprecated; use PATTERN instead (which is matched against the entire value and is case sensitive).', E_USER_DEPRECATED);
162: return (bool) Strings::match($control->getValue(), $regexp);
163: }
164:
165:
166: 167: 168: 169:
170: public static function validatePattern(TextBase $control, $pattern)
171: {
172: return (bool) Strings::match($control->getValue(), "\x01^($pattern)\\z\x01u");
173: }
174:
175:
176: 177: 178: 179:
180: public static function validateInteger(TextBase $control)
181: {
182: if (Validators::isNumericInt($value = $control->getValue())) {
183: if (!is_float($tmp = $value * 1)) {
184: $control->setValue($tmp);
185: }
186: return TRUE;
187: }
188: return FALSE;
189: }
190:
191:
192: 193: 194: 195:
196: public static function validateFloat(TextBase $control)
197: {
198: $value = self::filterFloat($control->getValue());
199: if (Validators::isNumeric($value)) {
200: $control->setValue((float) $value);
201: return TRUE;
202: }
203: return FALSE;
204: }
205:
206:
207: 208: 209: 210: 211:
212: public static function filterFloat($s)
213: {
214: return str_replace(array(' ', ','), array('', '.'), $s);
215: }
216:
217: }
218: