@@ -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,32 @@ 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
+ If set, the validator will check that the extension and the media type
158
+ (formerly known as MIME type) of the underlying file are equal to the given
159
+ extension and associated media type (if a string) or exist in the collection
160
+ (if an array).
161
+
162
+ By default, all media types associated with an extension are allowed.
163
+ The list of supported extensions and associated media types can be found on
164
+ the `IANA website `_.
165
+
166
+ It's also possible to explicitly configure the authorized media types for
167
+ an extension.
168
+
169
+ In the following example, allowed media types are explicitly set for the ``xml ``
170
+ and ``txt `` extensions, and all associated media types are allowed for ``jpg ``::
171
+
172
+ [
173
+ 'xml' => ['text/xml', 'application/xml'],
174
+ 'txt' => 'text/plain',
175
+ 'jpg',
176
+ ]
177
+
154
178
``disallowEmptyMessage ``
155
179
~~~~~~~~~~~~~~~~~~~~~~~~
156
180
@@ -216,9 +240,17 @@ Parameter Description
216
240
217
241
**type **: ``array `` or ``string ``
218
242
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).
243
+ .. seelalso ::
244
+
245
+ You should always use the ``extensions `` option instead of ``mimeTypes ``
246
+ except if you explicitly don't want to check that the extension of the file
247
+ is consistent with its content (this can be a security issue).
248
+
249
+ By default, the ``extensions `` option also checks the media type of the file.
250
+
251
+ If set, the validator will check that the media type (formerly known as MIME
252
+ type) of the underlying file is equal to the given mime type (if a string) or
253
+ exists in the collection of given mime types (if an array).
222
254
223
255
You can find a list of existing mime types on the `IANA website `_.
224
256
@@ -232,12 +264,22 @@ You can find a list of existing mime types on the `IANA website`_.
232
264
(i.e. the form type is not defined explicitly in the ``->add() `` method of
233
265
the form builder) and when the field doesn't define its own ``accept `` value.
234
266
267
+ ``extensionsMessage ``
268
+ ~~~~~~~~~~~~~~~~~~~~
269
+
270
+ **type **: ``string `` **default **: ``The extension of the file is invalid ({{ extension }}). Allowed extensions are {{ extensions }}. ``
271
+
272
+ The message displayed if the extension of the file is not a valid extension
273
+ per the `extensions `_ option.
274
+
275
+ .. include :: /reference/constraints/_parameters-mime-types-message-option.rst.inc
276
+
235
277
``mimeTypesMessage ``
236
278
~~~~~~~~~~~~~~~~~~~~
237
279
238
280
**type **: ``string `` **default **: ``The mime type of the file is invalid ({{ type }}). Allowed mime types are {{ types }}. ``
239
281
240
- The message displayed if the mime type of the file is not a valid mime type
282
+ The message displayed if the media type of the file is not a valid media type
241
283
per the `mimeTypes `_ option.
242
284
243
285
.. include :: /reference/constraints/_parameters-mime-types-message-option.rst.inc
0 commit comments