1: <?php
2:
3: 4: 5: 6:
7:
8: namespace Nette\Forms;
9:
10: use Nette;
11: use Nette\Utils\Strings;
12: use Nette\Utils\Validators;
13:
14:
15: 16: 17:
18: class Validator
19: {
20: use Nette\StaticClass;
21:
22:
23: public static $messages = [
24: Form::PROTECTION => 'Your session has expired. Please return to the home page and try again.',
25: Form::EQUAL => 'Please enter %s.',
26: Form::NOT_EQUAL => 'This value should not be %s.',
27: Form::FILLED => 'This field is required.',
28: Form::BLANK => 'This field should be blank.',
29: Form::MIN_LENGTH => 'Please enter at least %d characters.',
30: Form::MAX_LENGTH => 'Please enter no more than %d characters.',
31: Form::LENGTH => 'Please enter a value between %d and %d characters long.',
32: Form::EMAIL => 'Please enter a valid email address.',
33: Form::URL => 'Please enter a valid URL.',
34: Form::INTEGER => 'Please enter a valid integer.',
35: Form::FLOAT => 'Please enter a valid number.',
36: Form::MIN => 'Please enter a value greater than or equal to %d.',
37: Form::MAX => 'Please enter a value less than or equal to %d.',
38: Form::RANGE => 'Please enter a value between %d and %d.',
39: Form::MAX_FILE_SIZE => 'The size of the uploaded file can be up to %d bytes.',
40: Form::MAX_POST_SIZE => 'The uploaded data exceeds the limit of %d bytes.',
41: Form::MIME_TYPE => 'The uploaded file is not in the expected format.',
42: Form::IMAGE => 'The uploaded file must be image in format JPEG, GIF or PNG.',
43: Controls\SelectBox::VALID => 'Please select a valid option.',
44: Controls\UploadControl::VALID => 'An error occurred during file upload.',
45: ];
46:
47:
48:
49: public static function formatMessage(Rule $rule, $withValue = TRUE)
50: {
51: $message = $rule->message;
52: if ($message instanceof Nette\Utils\Html) {
53: return $message;
54:
55: } elseif ($message === NULL && is_string($rule->validator) && isset(static::$messages[$rule->validator])) {
56: $message = static::$messages[$rule->validator];
57:
58: } elseif ($message == NULL) {
59: trigger_error("Missing validation message for control '{$rule->control->getName()}'.", E_USER_WARNING);
60: }
61:
62: if ($translator = $rule->control->getForm()->getTranslator()) {
63: $message = $translator->translate($message, is_int($rule->arg) ? $rule->arg : NULL);
64: }
65:
66: $message = preg_replace_callback('#%(name|label|value|\d+\$[ds]|[ds])#', function ($m) use ($rule, $withValue) {
67: static $i = -1;
68: switch ($m[1]) {
69: case 'name': return $rule->control->getName();
70: case 'label': return $rule->control instanceof Controls\BaseControl ? $rule->control->translate($rule->control->caption) : NULL;
71: case 'value': return $withValue ? $rule->control->getValue() : $m[0];
72: default:
73: $args = is_array($rule->arg) ? $rule->arg : [$rule->arg];
74: $i = (int) $m[1] ? (int) $m[1] - 1 : $i + 1;
75: return isset($args[$i]) ? ($args[$i] instanceof IControl ? ($withValue ? $args[$i]->getValue() : "%$i") : $args[$i]) : '';
76: }
77: }, $message);
78: return $message;
79: }
80:
81:
82:
83:
84:
85: 86: 87: 88:
89: public static function validateEqual(IControl $control, $arg)
90: {
91: $value = $control->getValue();
92: foreach ((is_array($value) ? $value : [$value]) as $val) {
93: foreach ((is_array($arg) ? $arg : [$arg]) as $item) {
94: if ((string) $val === (string) $item) {
95: continue 2;
96: }
97: }
98: return FALSE;
99: }
100: return TRUE;
101: }
102:
103:
104: 105: 106: 107:
108: public static function validateNotEqual(IControl $control, $arg)
109: {
110: return !static::validateEqual($control, $arg);
111: }
112:
113:
114: 115: 116: 117:
118: public static function validateFilled(IControl $control)
119: {
120: return $control->isFilled();
121: }
122:
123:
124: 125: 126: 127:
128: public static function validateBlank(IControl $control)
129: {
130: return !$control->isFilled();
131: }
132:
133:
134: 135: 136: 137:
138: public static function validateValid(Controls\BaseControl $control)
139: {
140: return $control->getRules()->validate();
141: }
142:
143:
144: 145: 146: 147:
148: public static function validateRange(IControl $control, $range)
149: {
150: return Validators::isInRange($control->getValue(), $range);
151: }
152:
153:
154: 155: 156: 157:
158: public static function validateMin(IControl $control, $minimum)
159: {
160: return Validators::isInRange($control->getValue(), [$minimum, NULL]);
161: }
162:
163:
164: 165: 166: 167:
168: public static function validateMax(IControl $control, $maximum)
169: {
170: return Validators::isInRange($control->getValue(), [NULL, $maximum]);
171: }
172:
173:
174: 175: 176: 177:
178: public static function validateLength(IControl $control, $range)
179: {
180: if (!is_array($range)) {
181: $range = [$range, $range];
182: }
183: $value = $control->getValue();
184: return Validators::isInRange(is_array($value) ? count($value) : Strings::length($value), $range);
185: }
186:
187:
188: 189: 190: 191:
192: public static function validateMinLength(IControl $control, $length)
193: {
194: return static::validateLength($control, [$length, NULL]);
195: }
196:
197:
198: 199: 200: 201:
202: public static function validateMaxLength(IControl $control, $length)
203: {
204: return static::validateLength($control, [NULL, $length]);
205: }
206:
207:
208: 209: 210: 211:
212: public static function validateSubmitted(Controls\SubmitButton $control)
213: {
214: return $control->isSubmittedBy();
215: }
216:
217:
218: 219: 220: 221:
222: public static function validateEmail(IControl $control)
223: {
224: return Validators::isEmail($control->getValue());
225: }
226:
227:
228: 229: 230: 231:
232: public static function validateUrl(IControl $control)
233: {
234: if (Validators::isUrl($value = $control->getValue())) {
235: return TRUE;
236:
237: } elseif (Validators::isUrl($value = "http://$value")) {
238: $control->setValue($value);
239: return TRUE;
240: }
241: return FALSE;
242: }
243:
244:
245: 246: 247: 248:
249: public static function validatePattern(IControl $control, $pattern)
250: {
251: return (bool) Strings::match($control->getValue(), "\x01^(?:$pattern)\\z\x01u");
252: }
253:
254:
255: 256: 257: 258:
259: public static function validateInteger(IControl $control)
260: {
261: if (Validators::isNumericInt($value = $control->getValue())) {
262: if (!is_float($tmp = $value * 1)) {
263: $control->setValue($tmp);
264: }
265: return TRUE;
266: }
267: return FALSE;
268: }
269:
270:
271: 272: 273: 274:
275: public static function validateFloat(IControl $control)
276: {
277: $value = str_replace([' ', ','], ['', '.'], $control->getValue());
278: if (Validators::isNumeric($value)) {
279: $control->setValue((float) $value);
280: return TRUE;
281: }
282: return FALSE;
283: }
284:
285:
286: 287: 288: 289:
290: public static function validateFileSize(Controls\UploadControl $control, $limit)
291: {
292: foreach (static::toArray($control->getValue()) as $file) {
293: if ($file->getSize() > $limit || $file->getError() === UPLOAD_ERR_INI_SIZE) {
294: return FALSE;
295: }
296: }
297: return TRUE;
298: }
299:
300:
301: 302: 303: 304:
305: public static function validateMimeType(Controls\UploadControl $control, $mimeType)
306: {
307: $mimeTypes = is_array($mimeType) ? $mimeType : explode(',', $mimeType);
308: foreach (static::toArray($control->getValue()) as $file) {
309: $type = strtolower($file->getContentType());
310: if (!in_array($type, $mimeTypes, TRUE) && !in_array(preg_replace('#/.*#', '/*', $type), $mimeTypes, TRUE)) {
311: return FALSE;
312: }
313: }
314: return TRUE;
315: }
316:
317:
318: 319: 320: 321:
322: public static function validateImage(Controls\UploadControl $control)
323: {
324: foreach (static::toArray($control->getValue()) as $file) {
325: if (!$file->isImage()) {
326: return FALSE;
327: }
328: }
329: return TRUE;
330: }
331:
332:
333: 334: 335:
336: private static function toArray($value)
337: {
338: return $value instanceof Nette\Http\FileUpload ? [$value] : (array) $value;
339: }
340:
341: }
342: