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