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: return parent::getControl()->data('nette-empty-value', $this->emptyValue === '' ? NULL : $this->translate($this->emptyValue));
104: }
105:
106:
107:
108: public function notifyRule(Rule $rule)
109: {
110: if (is_string($rule->operation) && !$rule->isNegative) {
111: if (strcasecmp($rule->operation, ':float') === 0) {
112: $this->addFilter(callback(__CLASS__, 'filterFloat'));
113:
114: } elseif (strcasecmp($rule->operation, ':length') === 0) {
115: $this->control->maxlength = is_array($rule->arg) ? $rule->arg[1] : $rule->arg;
116:
117: } elseif (strcasecmp($rule->operation, ':maxLength') === 0) {
118: $this->control->maxlength = $rule->arg;
119: }
120: }
121:
122: parent::notifyRule($rule);
123: }
124:
125:
126:
127: 128: 129: 130: 131: 132:
133: public static function validateMinLength(TextBase $control, $length)
134: {
135: return String::length($control->getValue()) >= $length;
136: }
137:
138:
139:
140: 141: 142: 143: 144: 145:
146: public static function validateMaxLength(TextBase $control, $length)
147: {
148: return String::length($control->getValue()) <= $length;
149: }
150:
151:
152:
153: 154: 155: 156: 157: 158:
159: public static function validateLength(TextBase $control, $range)
160: {
161: if (!is_array($range)) {
162: $range = array($range, $range);
163: }
164: $len = String::length($control->getValue());
165: return ($range[0] === NULL || $len >= $range[0]) && ($range[1] === NULL || $len <= $range[1]);
166: }
167:
168:
169:
170: 171: 172: 173: 174:
175: public static function validateEmail(TextBase $control)
176: {
177: $atom = "[-a-z0-9!#$%&'*+/=?^_`{|}~]"; 178: $localPart = "(?:\"(?:[ !\\x23-\\x5B\\x5D-\\x7E]*|\\\\[ -~])+\"|$atom+(?:\\.$atom+)*)"; 179: $chars = "a-z0-9\x80-\xFF"; 180: $domain = "[$chars](?:[-$chars]{0,61}[$chars])"; 181: return (bool) String::match($control->getValue(), "(^$localPart@(?:$domain?\\.)+[-$chars]{2,19}\\z)i");
182: }
183:
184:
185:
186: 187: 188: 189: 190:
191: public static function validateUrl(TextBase $control)
192: {
193: $chars = "a-z0-9\x80-\xFF";
194: return (bool) String::match($control->getValue(), "#^(?:https?://|)(?:[$chars](?:[-$chars]{0,61}[$chars])?\\.)+[-$chars]{2,19}(/\S*)?$#i");
195: }
196:
197:
198:
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: public static function validateInteger(TextBase $control)
218: {
219: return (bool) String::match($control->getValue(), '/^-?[0-9]+$/');
220: }
221:
222:
223:
224: 225: 226: 227: 228:
229: public static function validateFloat(TextBase $control)
230: {
231: return (bool) String::match($control->getValue(), '/^-?[0-9]*[.,]?[0-9]+$/');
232: }
233:
234:
235:
236: 237: 238: 239: 240: 241:
242: public static function validateRange(TextBase $control, $range)
243: {
244: return ($range[0] === NULL || $control->getValue() >= $range[0]) && ($range[1] === NULL || $control->getValue() <= $range[1]);
245: }
246:
247:
248:
249: 250: 251: 252: 253:
254: public static function filterFloat($s)
255: {
256: return str_replace(array(' ', ','), array('', '.'), $s);
257: }
258:
259: }
260: