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