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

nette / forms / 26455457147

26 May 2026 02:44PM UTC coverage: 93.345%. Remained the same
26455457147

push

github

dg
fixed PHPStan errors

48 of 51 new or added lines in 12 files covered. (94.12%)

34 existing lines in 10 files now uncovered.

2104 of 2254 relevant lines covered (93.35%)

0.93 hits per line

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

95.68
/src/Forms/Validator.php
1
<?php declare(strict_types=1);
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
namespace Nette\Forms;
9

10
use Nette;
11
use Nette\Utils\Strings;
12
use Nette\Utils\Validators;
13
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;
14

15

16
/**
17
 * Common validators.
18
 */
19
final class Validator
20
{
21
        use Nette\StaticClass;
22

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

48

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

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

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

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

73
                $message = preg_replace_callback('#%(name|label|value|\d+\$[ds]|[ds])#', function (array $m) use ($rule, $withValue, $translator) {
1✔
74
                        static $i = -1;
1✔
75
                        switch ($m[1]) {
1✔
76
                                case 'name': return $rule->control->getName();
1✔
77
                                case 'label':
78
                                        if ($rule->control instanceof Controls\BaseControl) {
1✔
79
                                                $caption = $rule->control->getCaption();
1✔
80
                                                $caption = match (true) {
1✔
81
                                                        $caption instanceof Nette\Utils\Html => $caption->getText(),
1✔
82
                                                        $caption instanceof Nette\HtmlStringable => (string) $caption,
1✔
83
                                                        $translator !== null => $translator->translate($caption),
1✔
84
                                                        default => $caption,
1✔
85
                                                };
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
         * Checks whether the control's value equals the argument (string comparison, supports arrays).
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
         * Checks whether the control's value does not equal the argument.
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
         * Always returns the argument value, used for static (constant) conditions.
153
         */
154
        public static function validateStatic(Control $control, bool $arg): bool
1✔
155
        {
156
                return $arg;
1✔
157
        }
158

159

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

168

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

177

178
        /**
179
         * Checks whether the control passes all its validation rules (used in conditions).
180
         */
181
        public static function validateValid(Controls\BaseControl $control): bool
1✔
182
        {
183
                return $control->getRules()->validate();
1✔
184
        }
185

186

187
        /**
188
         * Checks whether the control's value falls within the specified range (inclusive).
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
         * Checks whether the control's value is greater than or equal to the 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
         * Checks whether the control's value is less than or equal to the 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
         * Checks whether the string length or array count falls within the given range [min, max].
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
         * Checks whether the string length or array count is at least the specified minimum.
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
         * Checks whether the string length or array count does not exceed the specified maximum.
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
         * Checks whether the submit button was used to submit the form.
254
         */
255
        public static function validateSubmitted(Controls\SubmitButton $control): bool
1✔
256
        {
257
                return $control->isSubmittedBy();
1✔
258
        }
259

260

261
        /**
262
         * Checks whether the control's value is a 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
         * Checks whether the control's value is a valid URL. Auto-prepends 'https://' if the scheme is missing.
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
         * Checks whether the control's value matches the regular expression (anchored, case-sensitive by default).
292
         */
293
        public static function validatePattern(Control $control, string $pattern, bool $caseInsensitive = false): bool
1✔
294
        {
295
                $regexp = "\x01^(?:$pattern)$\x01Du" . ($caseInsensitive ? 'i' : '');
1✔
296
                foreach (static::toArray($control->getValue()) as $item) {
1✔
297
                        $value = $item instanceof Nette\Http\FileUpload ? $item->getUntrustedName() : $item;
1✔
298
                        if (!Strings::match((string) $value, $regexp)) {
1✔
299
                                return false;
1✔
300
                        }
301
                }
302

303
                return true;
1✔
304
        }
305

306

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

312

313
        /**
314
         * Checks whether the control's value is a non-negative integer string or int.
315
         */
316
        public static function validateNumeric(Control $control): bool
1✔
317
        {
318
                $value = $control->getValue();
1✔
319
                return (is_int($value) && $value >= 0)
1✔
320
                        || (is_string($value) && Strings::match($value, '#^\d+$#D'));
1✔
321
        }
322

323

324
        /**
325
         * Checks whether the control's value is an integer. Normalizes the value by casting it to int.
326
         */
327
        public static function validateInteger(Control $control): bool
1✔
328
        {
329
                if (
330
                        Validators::isNumericInt($value = $control->getValue())
1✔
331
                        && !is_float($tmp = $value * 1) // too big for int?
1✔
332
                ) {
333
                        $control->setValue($tmp);
1✔
334
                        return true;
1✔
335
                }
336

337
                return false;
1✔
338
        }
339

340

341
        /**
342
         * Checks whether the control's value is a number. Normalizes spaces and commas and casts it to float.
343
         */
344
        public static function validateFloat(Control $control): bool
1✔
345
        {
346
                $value = $control->getValue();
1✔
347
                if (is_string($value)) {
1✔
348
                        $value = str_replace([' ', ','], ['', '.'], $value);
1✔
349
                }
350

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

356
                return false;
1✔
357
        }
358

359

360
        /**
361
         * Checks whether all uploaded files are within the size limit (in bytes).
362
         */
363
        public static function validateFileSize(Controls\UploadControl $control, int $limit): bool
1✔
364
        {
365
                foreach (static::toArray($control->getValue()) as $file) {
1✔
366
                        if ($file->getSize() > $limit || $file->getError() === UPLOAD_ERR_INI_SIZE) {
1✔
367
                                return false;
1✔
368
                        }
369
                }
370

371
                return true;
1✔
372
        }
373

374

375
        /**
376
         * Checks whether all uploaded files match one of the allowed MIME types (wildcards like 'image/*' are supported).
377
         * @param  string|string[]  $mimeType
378
         */
379
        public static function validateMimeType(Controls\UploadControl $control, string|array $mimeType): bool
1✔
380
        {
381
                $mimeTypes = is_array($mimeType) ? $mimeType : explode(',', $mimeType);
1✔
382
                foreach (static::toArray($control->getValue()) as $file) {
1✔
383
                        $type = strtolower($file->getContentType() ?? '');
1✔
384
                        if (!in_array($type, $mimeTypes, true) && !in_array(preg_replace('#/.*#', '/*', $type), $mimeTypes, true)) {
1✔
385
                                return false;
1✔
386
                        }
387
                }
388

389
                return true;
1✔
390
        }
391

392

393
        /**
394
         * Checks whether all uploaded files are images (JPEG, PNG, GIF, WebP, AVIF).
395
         */
396
        public static function validateImage(Controls\UploadControl $control): bool
1✔
397
        {
398
                foreach (static::toArray($control->getValue()) as $file) {
1✔
399
                        if (!$file->isImage()) {
1✔
UNCOV
400
                                return false;
×
401
                        }
402
                }
403

404
                return true;
1✔
405
        }
406

407

408
        /** @return mixed[] */
409
        private static function toArray(mixed $value): array
1✔
410
        {
411
                return is_object($value) ? [$value] : (array) $value;
1✔
412
        }
413
}
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