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