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

nette / forms / 28608018984

02 Jul 2026 05:08PM UTC coverage: 93.512% (+0.2%) from 93.28%
28608018984

push

github

dg
x

2854 of 3052 relevant lines covered (93.51%)

0.94 hits per line

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

97.06
/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, is_array, is_int, is_string;
14

15

16
/**
17
 * Default message catalog and legacy static validators.
18
 *
19
 * Message rendering lives in MessageFormatter; validator logic lives in the
20
 * Nette\Forms\Validation classes. The validateXxx() statics with an object
21
 * equivalent are deprecated thin delegates; the others remain because the
22
 * engine still needs them: Equal/NotEqual/Static/Valid/Submitted/Numeric
23
 * (operations without an object equivalent), Min/Max/Range (DateTimeControl
24
 * midnight wrap-around), MinLength/MaxLength/Length (item-count semantics
25
 * on multi-value controls) and Enum (invalid-class error path).
26
 */
27
final class Validator
28
{
29
        use Nette\StaticClass;
30

31
        /**
32
         * The immutable catalog of default validation messages.
33
         */
34
        public const Defaults = [
35
                Form::Equal => 'Please enter %s.',
36
                Form::NotEqual => 'This value should not be %s.',
37
                Form::Filled => 'This field is required.',
38
                Form::Blank => 'This field should be blank.',
39
                Form::MinLength => 'Please enter at least %d characters.',
40
                Form::MaxLength => 'Please enter no more than %d characters.',
41
                Form::Length => 'Please enter a value between %d and %d characters long.',
42
                Form::Email => 'Please enter a valid email address.',
43
                Form::URL => 'Please enter a valid URL.',
44
                Form::Enum => 'Please select a valid option.',
45
                Form::Integer => 'Please enter a valid integer.',
46
                Form::Float => 'Please enter a valid number.',
47
                Form::Min => 'Please enter a value greater than or equal to %d.',
48
                Form::Max => 'Please enter a value less than or equal to %d.',
49
                Form::Range => 'Please enter a value between %d and %d.',
50
                Form::MaxFileSize => 'The size of the uploaded file can be up to %d bytes.',
51
                Form::MaxPostSize => 'The uploaded data exceeds the limit of %limit bytes.',
52
                Form::MimeType => 'The uploaded file is not in the expected format.',
53
                Form::Image => 'The uploaded file must be image in format JPEG, GIF, PNG or WebP.',
54
                Controls\SelectBox::Valid => 'Please select a valid option.',
55
                Controls\UploadControl::Valid => 'An error occurred during file upload.',
56
                Controls\UploadControl::MaxFiles => 'The maximum allowed number of uploaded files is %count.',
57
                Repeater::MinItems => 'Please add at least %count items.',
58
                Repeater::MaxItems => 'Please add no more than %count items.',
59
        ];
60

61
        /**
62
         * App-wide overrides of the default validation messages; a changed entry takes
63
         * precedence over the default message of the mapped validator object.
64
         * @var array<string, string>
65
         */
66
        public static array $messages = self::Defaults;
67

68

69
        /**
70
         * Renders the failure message of the given rule.
71
         * @deprecated use Nette\Forms\MessageFormatter::format()
72
         */
73
        #[\Deprecated('use Nette\Forms\MessageFormatter::format()')]
74
        public static function formatMessage(Rule $rule, bool $withValue = true): string|Nette\HtmlStringable
75
        {
76
                return MessageFormatter::format($rule, $withValue);
×
77
        }
78

79

80
        /********************* default validators ****************d*g**/
81

82

83
        /**
84
         * Checks whether the control's value equals the argument (string comparison, supports arrays).
85
         */
86
        public static function validateEqual(Control $control, mixed $arg): bool
1✔
87
        {
88
                return (new Validation\EqualValidator($arg))->validate($control->getValue()) === null;
1✔
89
        }
90

91

92
        /**
93
         * Checks whether the control's value does not equal the argument.
94
         */
95
        public static function validateNotEqual(Control $control, mixed $arg): bool
1✔
96
        {
97
                return !static::validateEqual($control, $arg);
1✔
98
        }
99

100

101
        /**
102
         * Always returns the argument value, used for static (constant) conditions.
103
         */
104
        public static function validateStatic(Control $control, bool $arg): bool
1✔
105
        {
106
                return $arg;
1✔
107
        }
108

109

110
        /**
111
         * Checks whether the control is filled.
112
         * @deprecated use Nette\Forms\Validation\FilledValidator
113
         */
114
        #[\Deprecated('use Nette\Forms\Validation\FilledValidator')]
115
        public static function validateFilled(Control $control): bool
1✔
116
        {
117
                return (new Validation\FilledValidator)->validateControl($control) === null;
1✔
118
        }
119

120

121
        /**
122
         * Checks whether the control is not filled.
123
         * @deprecated use Nette\Forms\Validation\BlankValidator
124
         */
125
        #[\Deprecated('use Nette\Forms\Validation\BlankValidator')]
126
        public static function validateBlank(Control $control): bool
127
        {
128
                return (new Validation\BlankValidator)->validateControl($control) === null;
×
129
        }
130

131

132
        /**
133
         * Checks whether the control passes all its validation rules (used in conditions).
134
         */
135
        public static function validateValid(Control $control): bool
1✔
136
        {
137
                return $control->getRules()->validate();
1✔
138
        }
139

140

141
        /**
142
         * Checks whether the control's value falls within the specified range (inclusive).
143
         * @param  array{int|float|string|\DateTimeInterface|null, int|float|string|\DateTimeInterface|null}  $range
144
         */
145
        public static function validateRange(Control $control, array $range): bool
1✔
146
        {
147
                if ($control instanceof Controls\DateTimeControl) {
1✔
148
                        return $control->validateMinMax($range[0] ?? null, $range[1] ?? null);
1✔
149
                }
150
                $range = array_map(fn($v) => $v === '' ? null : $v, $range);
1✔
151
                return Validators::isInRange($control->getValue(), $range);
1✔
152
        }
153

154

155
        /**
156
         * Checks whether the control's value is greater than or equal to the minimum.
157
         */
158
        public static function validateMin(Control $control, int|float|string|\DateTimeInterface $minimum): bool
1✔
159
        {
160
                return Validators::isInRange($control->getValue(), [$minimum === '' ? null : $minimum, null]);
1✔
161
        }
162

163

164
        /**
165
         * Checks whether the control's value is less than or equal to the maximum.
166
         */
167
        public static function validateMax(Control $control, int|float|string|\DateTimeInterface $maximum): bool
1✔
168
        {
169
                return Validators::isInRange($control->getValue(), [null, $maximum === '' ? null : $maximum]);
1✔
170
        }
171

172

173
        /**
174
         * Checks whether the string length or array count falls within the given range [min, max].
175
         * @param  array{?int, ?int}|int  $range
176
         */
177
        public static function validateLength(Control $control, array|int $range): bool
1✔
178
        {
179
                if (!is_array($range)) {
1✔
180
                        $range = [$range, $range];
1✔
181
                }
182

183
                $value = $control->getValue();
1✔
184
                return Validators::isInRange(is_array($value) ? count($value) : Strings::length((string) $value), $range);
1✔
185
        }
186

187

188
        /**
189
         * Checks whether the string length or array count is at least the specified minimum.
190
         */
191
        public static function validateMinLength(Control $control, int $length): bool
1✔
192
        {
193
                return static::validateLength($control, [$length, null]);
1✔
194
        }
195

196

197
        /**
198
         * Checks whether the string length or array count does not exceed the specified maximum.
199
         */
200
        public static function validateMaxLength(Control $control, int $length): bool
1✔
201
        {
202
                return static::validateLength($control, [null, $length]);
1✔
203
        }
204

205

206
        /**
207
         * Checks whether the submit button was used to submit the form.
208
         */
209
        public static function validateSubmitted(Controls\SubmitButton $control): bool
1✔
210
        {
211
                return $control->isSubmittedBy();
1✔
212
        }
213

214

215
        /**
216
         * Checks whether the control's value is a valid email address.
217
         * @deprecated use Nette\Forms\Validation\EmailValidator
218
         */
219
        #[\Deprecated('use Nette\Forms\Validation\EmailValidator')]
220
        public static function validateEmail(Control $control): bool
1✔
221
        {
222
                return (new Validation\EmailValidator)->validate($control->getValue()) === null;
1✔
223
        }
224

225

226
        /**
227
         * Checks whether the control's value is a valid URL. Auto-prepends 'https://' if the scheme is missing.
228
         * @deprecated use Nette\Forms\Validation\UrlValidator
229
         */
230
        #[\Deprecated('use Nette\Forms\Validation\UrlValidator')]
231
        public static function validateUrl(Control $control): bool
1✔
232
        {
233
                $validator = new Validation\UrlValidator;
1✔
234
                if ($validator->validate($value = $control->getValue()) !== null) {
1✔
235
                        return false;
1✔
236
                }
237

238
                $control->setValue($validator->filter($value));
1✔
239
                return true;
1✔
240
        }
241

242

243
        /**
244
         * Checks whether the control's value matches the regular expression (anchored, case-sensitive by default).
245
         * @deprecated use Nette\Forms\Validation\PatternValidator
246
         */
247
        #[\Deprecated('use Nette\Forms\Validation\PatternValidator')]
248
        public static function validatePattern(Control $control, string $pattern, bool $caseInsensitive = false): bool
1✔
249
        {
250
                return (new Validation\PatternValidator($pattern, $caseInsensitive))->validate($control->getValue()) === null;
1✔
251
        }
252

253

254
        /** @deprecated use Nette\Forms\Validation\PatternValidator */
255
        #[\Deprecated('use Nette\Forms\Validation\PatternValidator')]
256
        public static function validatePatternCaseInsensitive(Control $control, string $pattern): bool
1✔
257
        {
258
                return (new Validation\PatternValidator($pattern, caseInsensitive: true))->validate($control->getValue()) === null;
1✔
259
        }
260

261

262
        /**
263
         * Checks whether the control's value is a valid case of the given backed enum.
264
         * @param  class-string  $enum  backed enum class
265
         */
266
        public static function validateEnum(Control $control, string $enum): bool
1✔
267
        {
268
                return (new Validation\EnumValidator($enum))->validate($control->getValue()) === null;
1✔
269
        }
270

271

272
        /**
273
         * Checks whether the control's value is a non-negative integer string or int.
274
         */
275
        public static function validateNumeric(Control $control): bool
1✔
276
        {
277
                $value = $control->getValue();
1✔
278
                return (is_int($value) && $value >= 0)
1✔
279
                        || (is_string($value) && Strings::match($value, '#^\d+$#D'));
1✔
280
        }
281

282

283
        /**
284
         * Checks whether the control's value is an integer. Normalizes the value by casting it to int.
285
         * @deprecated use Nette\Forms\Validation\IntegerValidator
286
         */
287
        #[\Deprecated('use Nette\Forms\Validation\IntegerValidator')]
288
        public static function validateInteger(Control $control): bool
1✔
289
        {
290
                $validator = new Validation\IntegerValidator;
1✔
291
                if ($validator->validate($value = $control->getValue()) !== null) {
1✔
292
                        return false;
1✔
293
                }
294

295
                $control->setValue($validator->filter($value));
1✔
296
                return true;
1✔
297
        }
298

299

300
        /**
301
         * Checks whether the control's value is a number. Normalizes spaces and commas and casts it to float.
302
         * @deprecated use Nette\Forms\Validation\FloatValidator
303
         */
304
        #[\Deprecated('use Nette\Forms\Validation\FloatValidator')]
305
        public static function validateFloat(Control $control): bool
1✔
306
        {
307
                $validator = new Validation\FloatValidator;
1✔
308
                if ($validator->validate($value = $control->getValue()) !== null) {
1✔
309
                        return false;
1✔
310
                }
311

312
                $control->setValue($validator->filter($value));
1✔
313
                return true;
1✔
314
        }
315

316

317
        /**
318
         * Checks whether all uploaded files are within the size limit (in bytes).
319
         * @deprecated use Nette\Forms\Validation\FileSizeValidator
320
         */
321
        #[\Deprecated('use Nette\Forms\Validation\FileSizeValidator')]
322
        public static function validateFileSize(Controls\UploadControl $control, int $limit): bool
1✔
323
        {
324
                return (new Validation\FileSizeValidator($limit))->validate($control->getValue()) === null;
1✔
325
        }
326

327

328
        /**
329
         * Checks whether all uploaded files match one of the allowed MIME types (wildcards like 'image/*' are supported).
330
         * @param  string|string[]  $mimeType
331
         * @deprecated use Nette\Forms\Validation\MimeTypeValidator
332
         */
333
        #[\Deprecated('use Nette\Forms\Validation\MimeTypeValidator')]
334
        public static function validateMimeType(Controls\UploadControl $control, string|array $mimeType): bool
1✔
335
        {
336
                return (new Validation\MimeTypeValidator($mimeType))->validate($control->getValue()) === null;
1✔
337
        }
338

339

340
        /**
341
         * Checks whether all uploaded files are images (JPEG, PNG, GIF, WebP, AVIF).
342
         * @deprecated use Nette\Forms\Validation\ImageValidator
343
         */
344
        #[\Deprecated('use Nette\Forms\Validation\ImageValidator')]
345
        public static function validateImage(Controls\UploadControl $control): bool
1✔
346
        {
347
                return (new Validation\ImageValidator)->validate($control->getValue()) === null;
1✔
348
        }
349
}
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