1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace Nette\Forms\Controls;
13:
14: use Nette,
15: Nette\Forms\Form,
16: Nette\Utils\Strings,
17: Nette\Utils\Validators;
18:
19:
20: 21: 22: 23: 24: 25: 26:
27: abstract class TextBase extends BaseControl
28: {
29:
30: protected $emptyValue = '';
31:
32:
33: protected $filters = array();
34:
35:
36: 37: 38:
39: public function __construct($label = NULL)
40: {
41: parent::__construct($label);
42: $this->addFilter($this->sanitize);
43: }
44:
45:
46: 47: 48: 49: 50:
51: public function setValue($value)
52: {
53: $this->value = is_array($value) ? '' : str_replace("\r\n", "\n", $value);
54: return $this;
55: }
56:
57:
58: 59: 60: 61:
62: public function getValue()
63: {
64: $value = $this->value;
65: foreach ($this->filters as $filter) {
66: $value = (string) $filter($value);
67: }
68: return $value === $this->translate($this->emptyValue) ? '' : $value;
69: }
70:
71:
72: 73: 74: 75: 76:
77: public function setEmptyValue($value)
78: {
79: $this->emptyValue = (string) $value;
80: return $this;
81: }
82:
83:
84: 85: 86: 87:
88: final public function getEmptyValue()
89: {
90: return $this->emptyValue;
91: }
92:
93:
94: 95: 96: 97: 98:
99: public function addFilter($filter)
100: {
101: $this->filters[] = new Nette\Callback($filter);
102: return $this;
103: }
104:
105:
106: 107: 108: 109:
110: public function sanitize($value)
111: {
112: if ($this->control->maxlength) {
113: $value = Nette\Utils\Strings::substring($value, 0, $this->control->maxlength);
114: }
115: if (strcasecmp($this->control->getName(), 'input') === 0) {
116: $value = Nette\Utils\Strings::trim(strtr($value, "\r\n", ' '));
117: }
118: return $value;
119: }
120:
121:
122: public function getControl()
123: {
124: $control = parent::getControl();
125: foreach ($this->getRules() as $rule) {
126: if ($rule->type === Nette\Forms\Rule::VALIDATOR && !$rule->isNegative
127: && ($rule->operation === Form::LENGTH || $rule->operation === Form::MAX_LENGTH)
128: ) {
129: $control->maxlength = is_array($rule->arg) ? $rule->arg[1] : $rule->arg;
130: }
131: }
132: if ($this->emptyValue !== '') {
133: $control->data('nette-empty-value', $this->translate($this->emptyValue));
134: }
135: return $control;
136: }
137:
138:
139: public function addRule($operation, $message = NULL, $arg = NULL)
140: {
141: if ($operation === Form::FLOAT) {
142: $this->addFilter(array(__CLASS__, 'filterFloat'));
143:
144: } elseif ($operation === Form::LENGTH || $operation === Form::MAX_LENGTH) {
145: $tmp = is_array($arg) ? $arg[1] : $arg;
146: $this->control->maxlength = is_scalar($tmp) ? $tmp : NULL;
147: }
148: return parent::addRule($operation, $message, $arg);
149: }
150:
151:
152: 153: 154: 155: 156: 157:
158: public static function validateMinLength(TextBase $control, $length)
159: {
160: return Strings::length($control->getValue()) >= $length;
161: }
162:
163:
164: 165: 166: 167: 168: 169:
170: public static function validateMaxLength(TextBase $control, $length)
171: {
172: return Strings::length($control->getValue()) <= $length;
173: }
174:
175:
176: 177: 178: 179: 180: 181:
182: public static function validateLength(TextBase $control, $range)
183: {
184: if (!is_array($range)) {
185: $range = array($range, $range);
186: }
187: return Validators::isInRange(Strings::length($control->getValue()), $range);
188: }
189:
190:
191: 192: 193: 194: 195:
196: public static function validateEmail(TextBase $control)
197: {
198: return Validators::isEmail($control->getValue());
199: }
200:
201:
202: 203: 204: 205: 206:
207: public static function validateUrl(TextBase $control)
208: {
209: return Validators::isUrl($control->getValue()) || Validators::isUrl('http://' . $control->getValue());
210: }
211:
212:
213:
214: public static function validateRegexp(TextBase $control, $regexp)
215: {
216: return (bool) Strings::match($control->getValue(), $regexp);
217: }
218:
219:
220: 221: 222: 223: 224: 225:
226: public static function validatePattern(TextBase $control, $pattern)
227: {
228: return (bool) Strings::match($control->getValue(), "\x01^($pattern)\\z\x01u");
229: }
230:
231:
232: 233: 234: 235: 236:
237: public static function validateInteger(TextBase $control)
238: {
239: return Validators::isNumericInt($control->getValue());
240: }
241:
242:
243: 244: 245: 246: 247:
248: public static function validateFloat(TextBase $control)
249: {
250: return Validators::isNumeric(static::filterFloat($control->getValue()));
251: }
252:
253:
254: 255: 256: 257: 258: 259:
260: public static function validateRange(TextBase $control, $range)
261: {
262: return Validators::isInRange($control->getValue(), $range);
263: }
264:
265:
266: 267: 268: 269: 270:
271: public static function filterFloat($s)
272: {
273: return str_replace(array(' ', ','), array('', '.'), $s);
274: }
275:
276: }
277: