@@ -67,8 +67,8 @@ below a certain file size and a valid PDF, add the following:
6767 {
6868 #[Assert\File(
6969 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',
7272 )]
7373 protected $bioFile;
7474 }
@@ -81,8 +81,8 @@ below a certain file size and a valid PDF, add the following:
8181 bioFile :
8282 - File :
8383 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
8686
8787 .. code-block :: xml
8888
@@ -96,11 +96,10 @@ below a certain file size and a valid PDF, add the following:
9696 <property name =" bioFile" >
9797 <constraint name =" File" >
9898 <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 >
102101 </option >
103- <option name =" mimeTypesMessage " >Please upload a valid PDF</option >
102+ <option name =" extensionsMessage " >Please upload a valid PDF</option >
104103 </constraint >
105104 </property >
106105 </class >
@@ -120,11 +119,10 @@ below a certain file size and a valid PDF, add the following:
120119 {
121120 $metadata->addPropertyConstraint('bioFile', new Assert\File([
122121 'maxSize' => '1024k',
123- 'mimeTypes' => [
124- 'application/pdf',
125- 'application/x-pdf',
122+ 'extensions' => [
123+ 'pdf',
126124 ],
127- 'mimeTypesMessage ' => 'Please upload a valid PDF',
125+ 'extensionsMessage ' => 'Please upload a valid PDF',
128126 ]));
129127 }
130128 }
@@ -151,6 +149,36 @@ the value defined in the ``maxSize`` option.
151149For more information about the difference between binary and SI prefixes,
152150see `Wikipedia: Binary prefix `_.
153151
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+
154182``disallowEmptyMessage ``
155183~~~~~~~~~~~~~~~~~~~~~~~~
156184
@@ -216,9 +244,17 @@ Parameter Description
216244
217245**type **: ``array `` or ``string ``
218246
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).
222258
223259You can find a list of existing mime types on the `IANA website `_.
224260
@@ -232,12 +268,26 @@ You can find a list of existing mime types on the `IANA website`_.
232268 (i.e. the form type is not defined explicitly in the ``->add() `` method of
233269 the form builder) and when the field doesn't define its own ``accept `` value.
234270
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+
235285``mimeTypesMessage ``
236286~~~~~~~~~~~~~~~~~~~~
237287
238288**type **: ``string `` **default **: ``The mime type of the file is invalid ({{ type }}). Allowed mime types are {{ types }}. ``
239289
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
241291per the `mimeTypes `_ option.
242292
243293.. include :: /reference/constraints/_parameters-mime-types-message-option.rst.inc
0 commit comments