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