Skip to content

Commit 1891edd

Browse files
author
Luciano Mammino
committed
Updated documentation for PR symfony/symfony#8490
1 parent a3710e1 commit 1891edd

File tree

1 file changed

+175
-7
lines changed

1 file changed

+175
-7
lines changed

reference/constraints/Image.rst

Lines changed: 175 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ automatically setup to work for image files specifically.
88
Additionally, as of Symfony 2.1, it has options so you can validate against
99
the width and height of the image.
1010

11+
.. versionadded:: 2.4
12+
As of Symfony 2.4 you can also validate against the image aspect ratio ( defined
13+
as ``width / height``) and selectively allow square, landscape and portrait
14+
image orientations.
15+
1116
See the :doc:`File</reference/constraints/File>` constraint for the bulk of
1217
the documentation on this constraint.
1318

@@ -19,12 +24,22 @@ the documentation on this constraint.
1924
| | - `maxWidth`_ |
2025
| | - `maxHeight`_ |
2126
| | - `minHeight`_ |
27+
| | - `maxRatio`_ |
28+
| | - `minRatio`_ |
29+
| | - `allowSquare`_ |
30+
| | - `allowLandscape`_ |
31+
| | - `allowPortrait`_ |
2232
| | - `mimeTypesMessage`_ |
2333
| | - `sizeNotDetectedMessage`_ |
2434
| | - `maxWidthMessage`_ |
2535
| | - `minWidthMessage`_ |
2636
| | - `maxHeightMessage`_ |
2737
| | - `minHeightMessage`_ |
38+
| | - `maxRatioMessage`_ |
39+
| | - `minRatioMessage`_ |
40+
| | - `allowSquareMessage`_ |
41+
| | - `allowLandscapeMessage`_ |
42+
| | - `allowPortraitMessage`_ |
2843
| | - See :doc:`File</reference/constraints/File>` for inherited options |
2944
+----------------+----------------------------------------------------------------------+
3045
| Class | :class:`Symfony\\Component\\Validator\\Constraints\\File` |
@@ -82,6 +97,8 @@ it is between a certain size, add the following:
8297
.. code-block:: php-annotations
8398
8499
// src/Acme/BlogBundle/Entity/Author.php
100+
namespace Acme\BlogBundle\Entity;
101+
85102
use Symfony\Component\Validator\Constraints as Assert;
86103
87104
class Author
@@ -114,18 +131,18 @@ it is between a certain size, add the following:
114131
.. code-block:: php
115132
116133
// src/Acme/BlogBundle/Entity/Author.php
117-
// ...
134+
namespace Acme/BlogBundle/Entity
118135
119136
use Symfony\Component\Validator\Mapping\ClassMetadata;
120-
use Symfony\Component\Validator\Constraints\Image;
137+
use Symfony\Component\Validator\Constraints as Assert;
121138
122139
class Author
123140
{
124141
// ...
125142
126143
public static function loadValidatorMetadata(ClassMetadata $metadata)
127144
{
128-
$metadata->addPropertyConstraint('headshot', new Image(array(
145+
$metadata->addPropertyConstraint('headshot', new Assert\Image(array(
129146
'minWidth' => 200,
130147
'maxWidth' => 400,
131148
'minHeight' => 200,
@@ -137,6 +154,76 @@ it is between a certain size, add the following:
137154
The ``headshot`` property is validated to guarantee that it is a real image
138155
and that it is between a certain width and height.
139156

157+
You may also want to guarantee the ``headshot`` image to be square. In this
158+
case you can disable portrait and landscape orientations as shown in the
159+
following code:
160+
161+
.. configuration-block::
162+
163+
.. code-block:: yaml
164+
165+
# src/Acme/BlogBundle/Resources/config/validation.yml
166+
Acme\BlogBundle\Entity\Author
167+
properties:
168+
headshot:
169+
- Image:
170+
allowLandscape: false
171+
allowPortrait: false
172+
173+
174+
.. code-block:: php-annotations
175+
176+
// src/Acme/BlogBundle/Entity/Author.php
177+
namespace Acme\BlogBundle\Entity;
178+
179+
use Symfony\Component\Validator\Constraints as Assert;
180+
181+
class Author
182+
{
183+
/**
184+
* @Assert\Image(
185+
* allowLandscape = false
186+
* allowPortrait = false
187+
* )
188+
*/
189+
protected $headshot;
190+
}
191+
192+
.. code-block:: xml
193+
194+
<!-- src/Acme/BlogBundle/Resources/config/validation.xml -->
195+
<class name="Acme\BlogBundle\Entity\Author">
196+
<property name="headshot">
197+
<constraint name="Image">
198+
<option name="allowLandscape">false</option>
199+
<option name="allowPortrait">false</option>
200+
</constraint>
201+
</property>
202+
</class>
203+
204+
.. code-block:: php
205+
206+
// src/Acme/BlogBundle/Entity/Author.php
207+
namespace Acme\BlogBundle\Entity;
208+
209+
use Symfony\Component\Validator\Mapping\ClassMetadata;
210+
use Symfony\Component\Validator\Constraints as Assert;
211+
212+
class Author
213+
{
214+
// ...
215+
216+
public static function loadValidatorMetadata(ClassMetadata $metadata)
217+
{
218+
$metadata->addPropertyConstraint('headshot', new Assert\Image(array(
219+
'allowLandscape' => false,
220+
'allowPortrait' => false,
221+
)));
222+
}
223+
}
224+
225+
You can mix all the constraint options to create powerful validation rules.
226+
140227
Options
141228
-------
142229

@@ -191,6 +278,43 @@ maxHeight
191278
If set, the height of the image file must be less than or equal to this
192279
value in pixels.
193280

281+
maxRatio
282+
~~~~~~~~
283+
284+
**type**: ``integer``
285+
286+
If set, the aspect ratio (``width / height``) of the image file must be less than or equal to this
287+
value.
288+
289+
minRatio
290+
~~~~~~~~
291+
292+
**type**: ``integer``
293+
294+
If set, the aspect ratio (``width / height``) of the image file must be greater than or equal to this
295+
value.
296+
297+
allowSquare
298+
~~~~~~~~~~~
299+
300+
**type**: ``Boolean`` **default**: ``true``
301+
302+
If this option is false, the image must not be square.
303+
304+
allowLandscape
305+
~~~~~~~~~~~~~~
306+
307+
**type**: ``Boolean`` **default**: ``true``
308+
309+
If this option is false, the image must not be landscape oriented.
310+
311+
allowPortrait
312+
~~~~~~~~~~~~~
313+
314+
**type**: ``Boolean`` **default**: ``true``
315+
316+
If this option is false, the image must not be portrait oriented.
317+
194318
sizeNotDetectedMessage
195319
~~~~~~~~~~~~~~~~~~~~~~
196320

@@ -203,29 +327,73 @@ options has been set.
203327
maxWidthMessage
204328
~~~~~~~~~~~~~~~
205329

206-
**type**: ``string`` **default**: ``The image width is too big ({{ width }}px). Allowed maximum width is {{ max_width }}px``
330+
**type**: ``string`` **default**: ``The image width is too big ({{ width }}px).
331+
Allowed maximum width is {{ max_width }}px``
207332

208333
The error message if the width of the image exceeds `maxWidth`_.
209334

210335
minWidthMessage
211336
~~~~~~~~~~~~~~~
212337

213-
**type**: ``string`` **default**: ``The image width is too small ({{ width }}px). Minimum width expected is {{ min_width }}px``
338+
**type**: ``string`` **default**: ``The image width is too small ({{ width }}px).
339+
Minimum width expected is {{ min_width }}px``
214340

215341
The error message if the width of the image is less than `minWidth`_.
216342

217343
maxHeightMessage
218344
~~~~~~~~~~~~~~~~
219345

220-
**type**: ``string`` **default**: ``The image height is too big ({{ height }}px). Allowed maximum height is {{ max_height }}px``
346+
**type**: ``string`` **default**: ``The image height is too big ({{ height }}px).
347+
Allowed maximum height is {{ max_height }}px``
221348

222349
The error message if the height of the image exceeds `maxHeight`_.
223350

224351
minHeightMessage
225352
~~~~~~~~~~~~~~~~
226353

227-
**type**: ``string`` **default**: ``The image height is too small ({{ height }}px). Minimum height expected is {{ min_height }}px``
354+
**type**: ``string`` **default**: ``The image height is too small ({{ height }}px).
355+
Minimum height expected is {{ min_height }}px``
228356

229357
The error message if the height of the image is less than `minHeight`_.
230358

359+
maxRatioMessage
360+
~~~~~~~~~~~~~~~
361+
362+
**type**: ``string`` **default**: ``The image ratio is too big ({{ ratio }}).
363+
Allowed maximum ratio is {{ max_ratio }}``
364+
365+
The error message if the aspect ratio of the image exceeds `maxRatio`_.
366+
367+
minRatioMessage
368+
~~~~~~~~~~~~~~~
369+
370+
**type**: ``string`` **default**: ``The image ratio is too small ({{ ratio }}).
371+
Minimum ratio expected is {{ min_ratio }}``
372+
373+
The error message if the aspect ratio of the image is less than `minRatio`_.
374+
375+
allowSquareMessage
376+
~~~~~~~~~~~~~~~~~~
377+
378+
**type**: ``string`` **default**: ``The image is square ({{ width }}x{{ height }}px).
379+
Square images are not allowed``
380+
381+
The error message if the image is square and you set `allowSquare`_ to ``false``.
382+
383+
allowLandscapeMessage
384+
~~~~~~~~~~~~~~~~~~~~~
385+
386+
**type**: ``string`` **default**: ``The image is landscape oriented ({{ width }}x{{ height }}px).
387+
Landscape oriented images are not allowed``
388+
389+
The error message if the image is landscape oriented and you set `allowLandscape`_ to ``false``.
390+
391+
allowPortraitMessage
392+
~~~~~~~~~~~~~~~~~~~~
393+
394+
**type**: ``string`` **default**: ``The image is portrait oriented ({{ width }}x{{ height }}px).
395+
Portrait oriented images are not allowed``
396+
397+
The error message if the image is portrait oriented and you set `allowPoirtrait`_ to ``false``.
398+
231399
.. _`IANA website`: http://www.iana.org/assignments/media-types/image/index.html

0 commit comments

Comments
 (0)