You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feature #58485 [Validator] Add filenameCharset and filenameCountUnit options to File constraint (IssamRaouf)
This PR was merged into the 7.3 branch.
Discussion
----------
[Validator] Add `filenameCharset` and `filenameCountUnit` options to `File` constraint
| Q | A
| ------------- | ---
| Branch? | 7.3
| Bug fix? | no
| New feature? | yes
| Deprecations? | no
| Issues | Fix#58482
| License | MIT
Commits
-------
abee6ae [Validator] Add `filenameCharset` and `filenameCountUnit` options to `File` constraint
publicstring$notFoundMessage = 'The file could not be found.';
57
74
publicstring$notReadableMessage = 'The file is not readable.';
58
75
publicstring$maxSizeMessage = 'The file is too large ({{ size }} {{ suffix }}). Allowed maximum size is {{ limit }} {{ suffix }}.';
59
76
publicstring$mimeTypesMessage = 'The mime type of the file is invalid ({{ type }}). Allowed mime types are {{ types }}.';
60
77
publicstring$extensionsMessage = 'The extension of the file is invalid ({{ extension }}). Allowed extensions are {{ extensions }}.';
61
78
publicstring$disallowEmptyMessage = 'An empty file is not allowed.';
62
79
publicstring$filenameTooLongMessage = 'The filename is too long. It should have {{ filename_max_length }} character or less.|The filename is too long. It should have {{ filename_max_length }} characters or less.';
80
+
publicstring$filenameCharsetMessage = 'This filename does not match the expected charset.';
63
81
64
82
publicstring$uploadIniSizeErrorMessage = 'The file is too large. Allowed maximum size is {{ limit }} {{ suffix }}.';
65
83
publicstring$uploadFormSizeErrorMessage = 'The file is too large.';
@@ -87,6 +105,8 @@ class File extends Constraint
87
105
* @param string|null $uploadErrorMessage Message if an unknown error occurred on upload
88
106
* @param string[]|null $groups
89
107
* @param array<string|string[]>|string|null $extensions A list of valid extensions to check. Related media types are also enforced ({@see https://symfony.com/doc/current/reference/constraints/File.html#extensions})
108
+
* @param string|null $filenameCharset The charset to be used when computing filename length (defaults to null)
109
+
* @param self::FILENAME_COUNT_*|null $filenameCountUnit The character count unit used for checking the filename length (defaults to {@see File::FILENAME_COUNT_BYTES})
90
110
*
91
111
* @see https://www.iana.org/assignments/media-types/media-types.xhtml Existing media types
92
112
*/
@@ -114,9 +134,11 @@ public function __construct(
114
134
?string$uploadErrorMessage = null,
115
135
?array$groups = null,
116
136
mixed$payload = null,
117
-
118
137
array|string|null$extensions = null,
119
138
?string$extensionsMessage = null,
139
+
?string$filenameCharset = null,
140
+
?string$filenameCountUnit = null,
141
+
?string$filenameCharsetMessage = null,
120
142
) {
121
143
if (\is_array($options)) {
122
144
trigger_deprecation('symfony/validator', '7.3', 'Passing an array of options to configure the "%s" constraint is deprecated, use named arguments instead.', static::class);
if (!\in_array($this->filenameCountUnit, self::FILENAME_VALID_COUNT_UNITS, true)) {
178
+
thrownewInvalidArgumentException(\sprintf('The "filenameCountUnit" option must be one of the "%s::FILENAME_COUNT_*" constants ("%s" given).', __CLASS__, $this->filenameCountUnit));
self::expectExceptionMessage(\sprintf('The "filenameCountUnit" option must be one of the "%s::FILENAME_COUNT_*" constants ("%s" given).', File::class, 'nonExistentCountUnit'));
yield'Simple case with only the parameter "filenameMaxLength" ' => [
696
+
yield'Codepoints and UTF-8 : default' => [
697
697
newFile(filenameMaxLength: 30),
698
+
'myFileWithATooLongOriginalFileName',
698
699
'The filename is too long. It should have {{ filename_max_length }} character or less.|The filename is too long. It should have {{ filename_max_length }} characters or less.',
699
700
];
700
701
701
-
yield'Case with the parameter "filenameMaxLength" and a custom error message' => [
702
-
newFile(filenameMaxLength: 20, filenameTooLongMessage: 'Your filename is too long. Please use at maximum {{ filename_max_length }} characters'),
703
-
'Your filename is too long. Please use at maximum {{ filename_max_length }} characters',
702
+
yield'Codepoints and UTF-8: custom error message' => [
0 commit comments