@@ -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,32 @@ 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+ 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+
154178``disallowEmptyMessage ``
155179~~~~~~~~~~~~~~~~~~~~~~~~
156180
@@ -216,9 +240,17 @@ Parameter Description
216240
217241**type **: ``array `` or ``string ``
218242
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).
222254
223255You can find a list of existing mime types on the `IANA website `_.
224256
@@ -232,12 +264,22 @@ You can find a list of existing mime types on the `IANA website`_.
232264 (i.e. the form type is not defined explicitly in the ``->add() `` method of
233265 the form builder) and when the field doesn't define its own ``accept `` value.
234266
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+
235277``mimeTypesMessage ``
236278~~~~~~~~~~~~~~~~~~~~
237279
238280**type **: ``string `` **default **: ``The mime type of the file is invalid ({{ type }}). Allowed mime types are {{ types }}. ``
239281
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
241283per the `mimeTypes `_ option.
242284
243285.. include :: /reference/constraints/_parameters-mime-types-message-option.rst.inc
0 commit comments