• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

nette / forms / 21851792823

10 Feb 2026 04:31AM UTC coverage: 93.412% (-0.07%) from 93.481%
21851792823

push

github

dg
component/model 4 WIP

2070 of 2216 relevant lines covered (93.41%)

0.93 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.62
/src/Forms/Validator.php
1
<?php
2

3
/**
4
 * This file is part of the Nette Framework (https://nette.org)
5
 * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
6
 */
7

8
declare(strict_types=1);
9

10
namespace Nette\Forms;
11

12
use Nette;
13
use Nette\Utils\Strings;
14
use Nette\Utils\Validators;
15
use function array_map, count, explode, in_array, is_array, is_float, is_int, is_object, is_string, preg_replace, preg_replace_callback, rtrim, str_replace, strtolower;
16
use const UPLOAD_ERR_INI_SIZE;
17

18

19
/**
20
 * Common validators.
21
 */
22
final class Validator
23
{
24
        use Nette\StaticClass;
25

26
        /** @var array<string, string> */
27
        public static array $messages = [
28
                Controls\CsrfProtection::Protection => 'Your session has expired. Please return to the home page and try again.',
29
                Form::Equal => 'Please enter %s.',
30
                Form::NotEqual => 'This value should not be %s.',
31
                Form::Filled => 'This field is required.',
32
                Form::Blank => 'This field should be blank.',
33
                Form::MinLength => 'Please enter at least %d characters.',
34
                Form::MaxLength => 'Please enter no more than %d characters.',
35
                Form::Length => 'Please enter a value between %d and %d characters long.',
36
                Form::Email => 'Please enter a valid email address.',
37
                Form::URL => 'Please enter a valid URL.',
38
                Form::Integer => 'Please enter a valid integer.',
39
                Form::Float => 'Please enter a valid number.',
40
                Form::Min => 'Please enter a value greater than or equal to %d.',
41
                Form::Max => 'Please enter a value less than or equal to %d.',
42
                Form::Range => 'Please enter a value between %d and %d.',
43
                Form::MaxFileSize => 'The size of the uploaded file can be up to %d bytes.',
44
                Form::MaxPostSize => 'The uploaded data exceeds the limit of %d bytes.',
45
                Form::MimeType => 'The uploaded file is not in the expected format.',
46
                Form::Image => 'The uploaded file must be image in format JPEG, GIF, PNG or WebP.',
47
                Controls\SelectBox::Valid => 'Please select a valid option.',
48
                Controls\UploadControl::Valid => 'An error occurred during file upload.',
49
        ];
50

51

52
        /**
53
         * @internal
54
         */
55
        public static function formatMessage(Rule $rule, bool $withValue = true): string|Nette\HtmlStringable
1✔
56
        {
57
                $message = $rule->message;
1✔
58
                if ($message instanceof Nette\HtmlStringable) {
1✔
59
                        return $message;
×
60

61
                } elseif ($message === null && is_string($rule->validator) && isset(static::$messages[$rule->validator])) {
1✔
62
                        $message = static::$messages[$rule->validator];
1✔
63

64
                } elseif ($message == null) { // intentionally ==
1✔
65
                        trigger_error(
×
66
                                "Missing validation message for control '{$rule->control->getName()}'"
×
67
                                . (is_string($rule->validator) ? " (validator '{$rule->validator}')." : '.'),
×
68
                                E_USER_WARNING,
×
69
                        );
70
                }
71

72
                if ($translator = $rule->control->getForm()->getTranslator()) {
1✔
73
                        $message = $translator->translate($message, is_int($rule->arg) ? $rule->arg : null);
1✔
74
                }
75

76
                $message = preg_replace_callback('#%(name|label|value|\d+\$[ds]|[ds])#', function (array $m) use ($rule, $withValue, $translator) {
1✔
77
                        static $i = -1;
1✔
78
                        switch ($m[1]) {
1✔
79
                                case 'name': return $rule->control->getName();
1✔
80
                                case 'label':
81
                                        if ($rule->control instanceof Controls\BaseControl) {
1✔
82
                                                $caption = $rule->control->getCaption();
1✔
83
                                                $caption = $caption instanceof Nette\HtmlStringable
1✔
84
                                                        ? $caption->getText()
1✔
85
                                                        : ($translator ? $translator->translate($caption) : $caption);
1✔
86
                                                return rtrim((string) $caption, ':');
1✔
87
                                        }
88

89
                                        return '';
90
                                case 'value': return $withValue
1✔
91
                                                ? $rule->control->getValue()
1✔
92
                                                : $m[0];
1✔
93
                                default:
94
                                        $args = is_array($rule->arg) ? $rule->arg : [$rule->arg];
1✔
95
                                        $i = (int) $m[1] ? (int) $m[1] - 1 : $i + 1;
1✔
96
                                        $arg = $args[$i] ?? null;
1✔
97
                                        if ($arg === null) {
1✔
98
                                                return '';
1✔
99
                                        } elseif ($arg instanceof Control) {
1✔
100
                                                return $withValue ? $args[$i]->getValue() : "%$i";
1✔
101
                                        } elseif ($rule->control instanceof Controls\DateTimeControl) {
1✔
102
                                                return $rule->control->formatLocaleText($arg);
1✔
103
                                        } else {
104
                                                return $arg;
1✔
105
                                        }
106
                        }
107
                }, $message);
1✔
108
                return $message;
1✔
109
        }
110

111

112
        /********************* default validators ****************d*g**/
113

114

115
        /**
116
         * Is control's value equal with second parameter?
117
         */
118
        public static function validateEqual(Control $control, mixed $arg): bool
1✔
119
        {
120
                $value = $control->getValue();
1✔
121
                $values = is_array($value) ? $value : [$value];
1✔
122
                $args = is_array($arg) ? $arg : [$arg];
1✔
123

124
                foreach ($values as $val) {
1✔
125
                        foreach ($args as $item) {
1✔
126
                                if ($item instanceof \BackedEnum) {
1✔
127
                                        $item = $item->value;
1✔
128
                                }
129

130
                                if ((string) $val === (string) $item) {
1✔
131
                                        continue 2;
1✔
132
                                }
133
                        }
134

135
                        return false;
1✔
136
                }
137

138
                return (bool) $values;
1✔
139
        }
140

141

142
        /**
143
         * Is control's value not equal with second parameter?
144
         */
145
        public static function validateNotEqual(Control $control, mixed $arg): bool
1✔
146
        {
147
                return !static::validateEqual($control, $arg);
1✔
148
        }
149

150

151
        /**
152
         * Returns argument.
153
         */
154
        public static function validateStatic(Control $control, bool $arg): bool
1✔
155
        {
156
                return $arg;
1✔
157
        }
158

159

160
        /**
161
         * Is control filled?
162
         */
163
        public static function validateFilled(Controls\BaseControl $control): bool
1✔
164
        {
165
                return $control->isFilled();
1✔
166
        }
167

168

169
        /**
170
         * Is control not filled?
171
         */
172
        public static function validateBlank(Controls\BaseControl $control): bool
1✔
173
        {
174
                return !$control->isFilled();
1✔
175
        }
176

177

178
        /**
179
         * Is control valid?
180
         */
181
        public static function validateValid(Controls\BaseControl $control): bool
1✔
182
        {
183
                return $control->getRules()->validate();
1✔
184
        }
185

186

187
        /**
188
         * Is a control's value number in specified range?
189
         * @param  array{int|float|string|\DateTimeInterface|null, int|float|string|\DateTimeInterface|null}  $range
190
         */
191
        public static function validateRange(Control $control, array $range): bool
1✔
192
        {
193
                if ($control instanceof Controls\DateTimeControl) {
1✔
194
                        return $control->validateMinMax($range[0] ?? null, $range[1] ?? null);
1✔
195
                }
196
                $range = array_map(fn($v) => $v === '' ? null : $v, $range);
1✔
197
                return Validators::isInRange($control->getValue(), $range);
1✔
198
        }
199

200

201
        /**
202
         * Is a control's value number greater than or equal to the specified minimum?
203
         */
204
        public static function validateMin(Control $control, int|float|string|\DateTimeInterface $minimum): bool
1✔
205
        {
206
                return Validators::isInRange($control->getValue(), [$minimum === '' ? null : $minimum, null]);
1✔
207
        }
208

209

210
        /**
211
         * Is a control's value number less than or equal to the specified maximum?
212
         */
213
        public static function validateMax(Control $control, int|float|string|\DateTimeInterface $maximum): bool
1✔
214
        {
215
                return Validators::isInRange($control->getValue(), [null, $maximum === '' ? null : $maximum]);
1✔
216
        }
217

218

219
        /**
220
         * Count/length validator. Range is array, min and max length pair.
221
         * @param  array{?int, ?int}|int  $range
222
         */
223
        public static function validateLength(Control $control, array|int $range): bool
1✔
224
        {
225
                if (!is_array($range)) {
1✔
226
                        $range = [$range, $range];
1✔
227
                }
228

229
                $value = $control->getValue();
1✔
230
                return Validators::isInRange(is_array($value) ? count($value) : Strings::length((string) $value), $range);
1✔
231
        }
232

233

234
        /**
235
         * Has control's value minimal count/length?
236
         */
237
        public static function validateMinLength(Control $control, int $length): bool
1✔
238
        {
239
                return static::validateLength($control, [$length, null]);
1✔
240
        }
241

242

243
        /**
244
         * Is control's value count/length in limit?
245
         */
246
        public static function validateMaxLength(Control $control, int $length): bool
1✔
247
        {
248
                return static::validateLength($control, [null, $length]);
1✔
249
        }
250

251

252
        /**
253
         * Has been button pressed?
254
         */
255
        public static function validateSubmitted(Controls\SubmitButton $control): bool
1✔
256
        {
257
                return $control->isSubmittedBy();
1✔
258
        }
259

260

261
        /**
262
         * Is control's value valid email address?
263
         */
264
        public static function validateEmail(Control $control): bool
1✔
265
        {
266
                return Validators::isEmail((string) $control->getValue());
1✔
267
        }
268

269

270
        /**
271
         * Is control's value valid URL?
272
         */
273
        public static function validateUrl(Control $control): bool
1✔
274
        {
275
                $value = (string) $control->getValue();
1✔
276
                if (Validators::isUrl($value)) {
1✔
277
                        return true;
1✔
278
                }
279

280
                $value = "https://$value";
1✔
281
                if (Validators::isUrl($value)) {
1✔
282
                        $control->setValue($value);
1✔
283
                        return true;
1✔
284
                }
285

286
                return false;
1✔
287
        }
288

289

290
        /**
291
         * Does the control's value match the regular expression?
292
         * Case-sensitive to comply with the HTML5 <input /> pattern attribute behaviour
293
         */
294
        public static function validatePattern(Control $control, string $pattern, bool $caseInsensitive = false): bool
1✔
295
        {
296
                $regexp = "\x01^(?:$pattern)$\x01Du" . ($caseInsensitive ? 'i' : '');
1✔
297
                foreach (static::toArray($control->getValue()) as $item) {
1✔
298
                        $value = $item instanceof Nette\Http\FileUpload ? $item->getUntrustedName() : $item;
1✔
299
                        if (!Strings::match((string) $value, $regexp)) {
1✔
300
                                return false;
1✔
301
                        }
302
                }
303

304
                return true;
1✔
305
        }
306

307

308
        public static function validatePatternCaseInsensitive(Control $control, string $pattern): bool
1✔
309
        {
310
                return self::validatePattern($control, $pattern, caseInsensitive: true);
1✔
311
        }
312

313

314
        /**
315
         * Is a control's value numeric?
316
         */
317
        public static function validateNumeric(Control $control): bool
1✔
318
        {
319
                $value = $control->getValue();
1✔
320
                return (is_int($value) && $value >= 0)
1✔
321
                        || (is_string($value) && Strings::match($value, '#^\d+$#D'));
1✔
322
        }
323

324

325
        /**
326
         * Is a control's value decimal number?
327
         */
328
        public static function validateInteger(Control $control): bool
1✔
329
        {
330
                if (
331
                        Validators::isNumericInt($value = $control->getValue())
1✔
332
                        && !is_float($tmp = $value * 1) // too big for int?
1✔
333
                ) {
334
                        $control->setValue($tmp);
1✔
335
                        return true;
1✔
336
                }
337

338
                return false;
1✔
339
        }
340

341

342
        /**
343
         * Is a control's value float number?
344
         */
345
        public static function validateFloat(Control $control): bool
1✔
346
        {
347
                $value = $control->getValue();
1✔
348
                if (is_string($value)) {
1✔
349
                        $value = str_replace([' ', ','], ['', '.'], $value);
1✔
350
                }
351

352
                if (Validators::isNumeric($value)) {
1✔
353
                        $control->setValue((float) $value);
1✔
354
                        return true;
1✔
355
                }
356

357
                return false;
1✔
358
        }
359

360

361
        /**
362
         * Is file size in limit?
363
         */
364
        public static function validateFileSize(Controls\UploadControl $control, int $limit): bool
1✔
365
        {
366
                foreach (static::toArray($control->getValue()) as $file) {
1✔
367
                        if ($file->getSize() > $limit || $file->getError() === UPLOAD_ERR_INI_SIZE) {
1✔
368
                                return false;
1✔
369
                        }
370
                }
371

372
                return true;
1✔
373
        }
374

375

376
        /**
377
         * Has file specified mime type?
378
         * @param  string|string[]  $mimeType
379
         */
380
        public static function validateMimeType(Controls\UploadControl $control, string|array $mimeType): bool
1✔
381
        {
382
                $mimeTypes = is_array($mimeType) ? $mimeType : explode(',', $mimeType);
1✔
383
                foreach (static::toArray($control->getValue()) as $file) {
1✔
384
                        $type = strtolower($file->getContentType() ?? '');
1✔
385
                        if (!in_array($type, $mimeTypes, true) && !in_array(preg_replace('#/.*#', '/*', $type), $mimeTypes, true)) {
1✔
386
                                return false;
1✔
387
                        }
388
                }
389

390
                return true;
1✔
391
        }
392

393

394
        /**
395
         * Is file image?
396
         */
397
        public static function validateImage(Controls\UploadControl $control): bool
1✔
398
        {
399
                foreach (static::toArray($control->getValue()) as $file) {
1✔
400
                        if (!$file->isImage()) {
1✔
401
                                return false;
×
402
                        }
403
                }
404

405
                return true;
1✔
406
        }
407

408

409
        /** @return mixed[] */
410
        private static function toArray(mixed $value): array
1✔
411
        {
412
                return is_object($value) ? [$value] : (array) $value;
1✔
413
        }
414
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc