@@ -67,8 +67,8 @@ below a certain file size and a valid PDF, add the following:
67
67
{
68
68
#[Assert\File(
69
69
maxSize: '1024k',
70
- mimeTypes : ['application/pdf', 'application/x- pdf'],
71
- mimeTypesMessage : 'Please upload a valid PDF',
70
+ extensions : ['pdf'],
71
+ extensionsMessage : 'Please upload a valid PDF',
72
72
)]
73
73
protected $bioFile;
74
74
}
@@ -81,8 +81,8 @@ below a certain file size and a valid PDF, add the following:
81
81
bioFile :
82
82
- File :
83
83
maxSize : 1024k
84
- mimeTypes : [application/pdf, application/x- pdf]
85
- mimeTypesMessage : Please upload a valid PDF
84
+ extensions : [pdf]
85
+ extensionsMessage : Please upload a valid PDF
86
86
87
87
.. code-block :: xml
88
88
@@ -96,11 +96,10 @@ below a certain file size and a valid PDF, add the following:
96
96
<property name =" bioFile" >
97
97
<constraint name =" File" >
98
98
<option name =" maxSize" >1024k</option >
99
- <option name =" mimeTypes" >
100
- <value >application/pdf</value >
101
- <value >application/x-pdf</value >
99
+ <option name =" extensions" >
100
+ <value >pdf</value >
102
101
</option >
103
- <option name =" mimeTypesMessage " >Please upload a valid PDF</option >
102
+ <option name =" extensionsMessage " >Please upload a valid PDF</option >
104
103
</constraint >
105
104
</property >
106
105
</class >
@@ -120,11 +119,10 @@ below a certain file size and a valid PDF, add the following:
120
119
{
121
120
$metadata->addPropertyConstraint('bioFile', new Assert\File([
122
121
'maxSize' => '1024k',
123
- 'mimeTypes' => [
124
- 'application/pdf',
125
- 'application/x-pdf',
122
+ 'extensions' => [
123
+ 'pdf',
126
124
],
127
- 'mimeTypesMessage ' => 'Please upload a valid PDF',
125
+ 'extensionsMessage ' => 'Please upload a valid PDF',
128
126
]));
129
127
}
130
128
}
@@ -151,6 +149,36 @@ the value defined in the ``maxSize`` option.
151
149
For more information about the difference between binary and SI prefixes,
152
150
see `Wikipedia: Binary prefix `_.
153
151
152
+ ``extensions ``
153
+ ~~~~~~~~~~~~~~
154
+
155
+ **type **: ``array `` or ``string ``
156
+
157
+ .. versionadded :: 6.2
158
+
159
+ The ``extensions `` option was introduced in Symfony 6.2.
160
+
161
+ If set, the validator will check that the extension and the media type
162
+ (formerly known as MIME type) of the underlying file are equal to the given
163
+ extension and associated media type (if a string) or exist in the collection
164
+ (if an array).
165
+
166
+ By default, all media types associated with an extension are allowed.
167
+ The list of supported extensions and associated media types can be found on
168
+ the `IANA website `_.
169
+
170
+ It's also possible to explicitly configure the authorized media types for
171
+ an extension.
172
+
173
+ In the following example, allowed media types are explicitly set for the ``xml ``
174
+ and ``txt `` extensions, and all associated media types are allowed for ``jpg ``::
175
+
176
+ [
177
+ 'xml' => ['text/xml', 'application/xml'],
178
+ 'txt' => 'text/plain',
179
+ 'jpg',
180
+ ]
181
+
154
182
``disallowEmptyMessage ``
155
183
~~~~~~~~~~~~~~~~~~~~~~~~
156
184
@@ -216,9 +244,17 @@ Parameter Description
216
244
217
245
**type **: ``array `` or ``string ``
218
246
219
- If set, the validator will check that the mime type of the underlying file
220
- is equal to the given mime type (if a string) or exists in the collection
221
- of given mime types (if an array).
247
+ .. seelalso ::
248
+
249
+ You should always use the ``extensions `` option instead of ``mimeTypes ``
250
+ except if you explicitly don't want to check that the extension of the file
251
+ is consistent with its content (this can be a security issue).
252
+
253
+ By default, the ``extensions `` option also checks the media type of the file.
254
+
255
+ If set, the validator will check that the media type (formerly known as MIME
256
+ type) of the underlying file is equal to the given mime type (if a string) or
257
+ exists in the collection of given mime types (if an array).
222
258
223
259
You can find a list of existing mime types on the `IANA website `_.
224
260
@@ -232,12 +268,26 @@ You can find a list of existing mime types on the `IANA website`_.
232
268
(i.e. the form type is not defined explicitly in the ``->add() `` method of
233
269
the form builder) and when the field doesn't define its own ``accept `` value.
234
270
271
+ ``extensionsMessage ``
272
+ ~~~~~~~~~~~~~~~~~~~~~
273
+
274
+ **type **: ``string `` **default **: ``The extension of the file is invalid ({{ extension }}). Allowed extensions are {{ extensions }}. ``
275
+
276
+ .. versionadded :: 6.2
277
+
278
+ The ``extensionsMessage `` option was introduced in Symfony 6.3.
279
+
280
+ The message displayed if the extension of the file is not a valid extension
281
+ per the `extensions `_ option.
282
+
283
+ .. include :: /reference/constraints/_parameters-mime-types-message-option.rst.inc
284
+
235
285
``mimeTypesMessage ``
236
286
~~~~~~~~~~~~~~~~~~~~
237
287
238
288
**type **: ``string `` **default **: ``The mime type of the file is invalid ({{ type }}). Allowed mime types are {{ types }}. ``
239
289
240
- The message displayed if the mime type of the file is not a valid mime type
290
+ The message displayed if the media type of the file is not a valid media type
241
291
per the `mimeTypes `_ option.
242
292
243
293
.. include :: /reference/constraints/_parameters-mime-types-message-option.rst.inc
0 commit comments