Skip to content

Commit 9d7c999

Browse files
committed
feature #3895 [Validator] Support "maxSize" given in KiB (jeremy-derusse)
This PR was merged into the master branch. Discussion ---------- [Validator] Support "maxSize" given in KiB | Q | A | ------------- | --- | Doc fix? | no | New docs? | symfony/symfony#11027 | Applies to | 2.6 | Fixed tickets | symfony/symfony#10962 To display the constraint validation message with an expected suffix, this PR add a new option in File constraint. Commits ------- 1e8fa48 Replace bullet list by a table 77a0687 Add versionAdded on binary suffix b414f5c Use comma as thousands separator 7dcd7c3 Move secion "binaryFormat" to the right place 71fdd60 Fix line wrap 3f3f4e0 Provide information about SI and Binary prefixes deac0c3 Add option binaryFormat in constraint file e3acdc5 Support MaxSize in KiB and MiB
2 parents fdf2a95 + 1e8fa48 commit 9d7c999

File tree

1 file changed

+35
-7
lines changed

1 file changed

+35
-7
lines changed

reference/constraints/File.rst

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ form type.
2020
| Applies to | :ref:`property or method <validation-property-target>` |
2121
+----------------+---------------------------------------------------------------------+
2222
| Options | - `maxSize`_ |
23+
| | - `binaryFormat`_ |
2324
| | - `mimeTypes`_ |
2425
| | - `maxSizeMessage`_ |
2526
| | - `mimeTypesMessage`_ |
@@ -79,7 +80,6 @@ below a certain file size and a valid PDF, add the following:
7980
maxSize: 1024k
8081
mimeTypes: [application/pdf, application/x-pdf]
8182
mimeTypesMessage: Please upload a valid PDF
82-
8383
8484
.. code-block:: php-annotations
8585
@@ -155,19 +155,46 @@ Options
155155
maxSize
156156
~~~~~~~
157157

158+
.. versionadded:: 2.6
159+
The suffixes ``Ki`` and ``Mi`` were introduced in Symfony 2.6.
160+
158161
**type**: ``mixed``
159162

160163
If set, the size of the underlying file must be below this file size in order
161164
to be valid. The size of the file can be given in one of the following formats:
162165

163-
* **bytes**: To specify the ``maxSize`` in bytes, pass a value that is entirely
164-
numeric (e.g. ``4096``);
166+
+--------+-----------+-----------------+------+
167+
| Suffix | Unit Name | value | e.g. |
168+
+========+===========+=================+======+
169+
| | byte | 1 byte | 4096 |
170+
+--------+-----------+-----------------+------+
171+
| k | kilobyte | 1,000 bytes | 200k |
172+
+--------+-----------+-----------------+------+
173+
| M | megabyte | 1,000,000 bytes | 2M |
174+
+--------+-----------+-----------------+------+
175+
| Ki | kibibyte | 1,024 bytes | 32Ki |
176+
+--------+-----------+-----------------+------+
177+
| Mi | mebibyte | 1,048,576 bytes | 8Mi |
178+
+--------+-----------+-----------------+------+
179+
180+
For more information about the difference between binary and SI suffixes,
181+
see `Wikipedia: Binary prefix`_.
182+
183+
binaryFormat
184+
~~~~~~~~~~~~
185+
186+
.. versionadded:: 2.6
187+
The ``binaryFormat`` option was introduced in Symfony 2.6.
188+
189+
**type**: ``boolean`` **default**: ``null``
165190

166-
* **kilobytes**: To specify the ``maxSize`` in kilobytes, pass a number and
167-
suffix it with a lowercase "k" (e.g. ``200k``);
191+
When ``true``, the sizes will be displayed in messages with binary suffixes
192+
(KiB, MiB). When ``false``, the sizes will be displayed with SI suffixes (kB,
193+
MB). When ``null``, then the binaryFormat will be guessed from the suffix
194+
defined in the ``maxSize`` option.
168195

169-
* **megabytes**: To specify the ``maxSize`` in megabytes, pass a number and
170-
suffix it with a capital "M" (e.g. ``4M``).
196+
For more information about the difference between binary and SI suffixes,
197+
see `Wikipedia: Binary prefix`_.
171198

172199
mimeTypes
173200
~~~~~~~~~
@@ -251,3 +278,4 @@ to disk.
251278

252279

253280
.. _`IANA website`: http://www.iana.org/assignments/media-types/index.html
281+
.. _`Wikipedia: Binary prefix`: http://en.wikipedia.org/wiki/Binary_prefix

0 commit comments

Comments
 (0)