@@ -8,6 +8,11 @@ automatically setup to work for image files specifically.
8
8
Additionally it has options so you can validate against the width and height
9
9
of the image.
10
10
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
+
11
16
See the :doc: `File</reference/constraints/File> ` constraint for the bulk of
12
17
the documentation on this constraint.
13
18
@@ -19,12 +24,22 @@ the documentation on this constraint.
19
24
| | - `maxWidth `_ |
20
25
| | - `maxHeight `_ |
21
26
| | - `minHeight `_ |
27
+ | | - `maxRatio `_ |
28
+ | | - `minRatio `_ |
29
+ | | - `allowSquare `_ |
30
+ | | - `allowLandscape `_ |
31
+ | | - `allowPortrait `_ |
22
32
| | - `mimeTypesMessage `_ |
23
33
| | - `sizeNotDetectedMessage `_ |
24
34
| | - `maxWidthMessage `_ |
25
35
| | - `minWidthMessage `_ |
26
36
| | - `maxHeightMessage `_ |
27
37
| | - `minHeightMessage `_ |
38
+ | | - `maxRatioMessage `_ |
39
+ | | - `minRatioMessage `_ |
40
+ | | - `allowSquareMessage `_ |
41
+ | | - `allowLandscapeMessage `_ |
42
+ | | - `allowPortraitMessage `_ |
28
43
| | - See :doc: `File</reference/constraints/File> ` for inherited options |
29
44
+----------------+----------------------------------------------------------------------+
30
45
| Class | :class: `Symfony\\ Component\\ Validator\\ Constraints\\ File ` |
@@ -82,6 +97,8 @@ it is between a certain size, add the following:
82
97
.. code-block :: php-annotations
83
98
84
99
// src/Acme/BlogBundle/Entity/Author.php
100
+ namespace Acme\BlogBundle\Entity;
101
+
85
102
use Symfony\Component\Validator\Constraints as Assert;
86
103
87
104
class Author
@@ -120,18 +137,18 @@ it is between a certain size, add the following:
120
137
.. code-block :: php
121
138
122
139
// src/Acme/BlogBundle/Entity/Author.php
123
- // ...
140
+ namespace Acme/BlogBundle/Entity
124
141
125
142
use Symfony\Component\Validator\Mapping\ClassMetadata;
126
- use Symfony\Component\Validator\Constraints\Image ;
143
+ use Symfony\Component\Validator\Constraints as Assert ;
127
144
128
145
class Author
129
146
{
130
147
// ...
131
148
132
149
public static function loadValidatorMetadata(ClassMetadata $metadata)
133
150
{
134
- $metadata->addPropertyConstraint('headshot', new Image(array(
151
+ $metadata->addPropertyConstraint('headshot', new Assert\ Image(array(
135
152
'minWidth' => 200,
136
153
'maxWidth' => 400,
137
154
'minHeight' => 200,
@@ -143,6 +160,76 @@ it is between a certain size, add the following:
143
160
The ``headshot `` property is validated to guarantee that it is a real image
144
161
and that it is between a certain width and height.
145
162
163
+ You may also want to guarantee the ``headshot `` image to be square. In this
164
+ case you can disable portrait and landscape orientations as shown in the
165
+ following code:
166
+
167
+ .. configuration-block ::
168
+
169
+ .. code-block :: yaml
170
+
171
+ # src/Acme/BlogBundle/Resources/config/validation.yml
172
+ Acme\BlogBundle\Entity\Author
173
+ properties :
174
+ headshot :
175
+ - Image :
176
+ allowLandscape : false
177
+ allowPortrait : false
178
+
179
+
180
+ .. code-block :: php-annotations
181
+
182
+ // src/Acme/BlogBundle/Entity/Author.php
183
+ namespace Acme\BlogBundle\Entity;
184
+
185
+ use Symfony\Component\Validator\Constraints as Assert;
186
+
187
+ class Author
188
+ {
189
+ /**
190
+ * @Assert\Image(
191
+ * allowLandscape = false
192
+ * allowPortrait = false
193
+ * )
194
+ */
195
+ protected $headshot;
196
+ }
197
+
198
+ .. code-block :: xml
199
+
200
+ <!-- src/Acme/BlogBundle/Resources/config/validation.xml -->
201
+ <class name =" Acme\BlogBundle\Entity\Author" >
202
+ <property name =" headshot" >
203
+ <constraint name =" Image" >
204
+ <option name =" allowLandscape" >false</option >
205
+ <option name =" allowPortrait" >false</option >
206
+ </constraint >
207
+ </property >
208
+ </class >
209
+
210
+ .. code-block :: php
211
+
212
+ // src/Acme/BlogBundle/Entity/Author.php
213
+ namespace Acme\BlogBundle\Entity;
214
+
215
+ use Symfony\Component\Validator\Mapping\ClassMetadata;
216
+ use Symfony\Component\Validator\Constraints as Assert;
217
+
218
+ class Author
219
+ {
220
+ // ...
221
+
222
+ public static function loadValidatorMetadata(ClassMetadata $metadata)
223
+ {
224
+ $metadata->addPropertyConstraint('headshot', new Assert\Image(array(
225
+ 'allowLandscape' => false,
226
+ 'allowPortrait' => false,
227
+ )));
228
+ }
229
+ }
230
+
231
+ You can mix all the constraint options to create powerful validation rules.
232
+
146
233
Options
147
234
-------
148
235
@@ -194,6 +281,43 @@ maxHeight
194
281
If set, the height of the image file must be less than or equal to this
195
282
value in pixels.
196
283
284
+ maxRatio
285
+ ~~~~~~~~
286
+
287
+ **type **: ``integer ``
288
+
289
+ If set, the aspect ratio (``width / height ``) of the image file must be less than or equal to this
290
+ value.
291
+
292
+ minRatio
293
+ ~~~~~~~~
294
+
295
+ **type **: ``integer ``
296
+
297
+ If set, the aspect ratio (``width / height ``) of the image file must be greater than or equal to this
298
+ value.
299
+
300
+ allowSquare
301
+ ~~~~~~~~~~~
302
+
303
+ **type **: ``Boolean `` **default **: ``true ``
304
+
305
+ If this option is false, the image must not be square.
306
+
307
+ allowLandscape
308
+ ~~~~~~~~~~~~~~
309
+
310
+ **type **: ``Boolean `` **default **: ``true ``
311
+
312
+ If this option is false, the image must not be landscape oriented.
313
+
314
+ allowPortrait
315
+ ~~~~~~~~~~~~~
316
+
317
+ **type **: ``Boolean `` **default **: ``true ``
318
+
319
+ If this option is false, the image must not be portrait oriented.
320
+
197
321
sizeNotDetectedMessage
198
322
~~~~~~~~~~~~~~~~~~~~~~
199
323
@@ -206,29 +330,73 @@ options has been set.
206
330
maxWidthMessage
207
331
~~~~~~~~~~~~~~~
208
332
209
- **type **: ``string `` **default **: ``The image width is too big ({{ width }}px). Allowed maximum width is {{ max_width }}px ``
333
+ **type **: ``string `` **default **: ``The image width is too big ({{ width }}px).
334
+ Allowed maximum width is {{ max_width }}px ``
210
335
211
336
The error message if the width of the image exceeds `maxWidth `_.
212
337
213
338
minWidthMessage
214
339
~~~~~~~~~~~~~~~
215
340
216
- **type **: ``string `` **default **: ``The image width is too small ({{ width }}px). Minimum width expected is {{ min_width }}px ``
341
+ **type **: ``string `` **default **: ``The image width is too small ({{ width }}px).
342
+ Minimum width expected is {{ min_width }}px ``
217
343
218
344
The error message if the width of the image is less than `minWidth `_.
219
345
220
346
maxHeightMessage
221
347
~~~~~~~~~~~~~~~~
222
348
223
- **type **: ``string `` **default **: ``The image height is too big ({{ height }}px). Allowed maximum height is {{ max_height }}px ``
349
+ **type **: ``string `` **default **: ``The image height is too big ({{ height }}px).
350
+ Allowed maximum height is {{ max_height }}px ``
224
351
225
352
The error message if the height of the image exceeds `maxHeight `_.
226
353
227
354
minHeightMessage
228
355
~~~~~~~~~~~~~~~~
229
356
230
- **type **: ``string `` **default **: ``The image height is too small ({{ height }}px). Minimum height expected is {{ min_height }}px ``
357
+ **type **: ``string `` **default **: ``The image height is too small ({{ height }}px).
358
+ Minimum height expected is {{ min_height }}px ``
231
359
232
360
The error message if the height of the image is less than `minHeight `_.
233
361
362
+ maxRatioMessage
363
+ ~~~~~~~~~~~~~~~
364
+
365
+ **type **: ``string `` **default **: ``The image ratio is too big ({{ ratio }}).
366
+ Allowed maximum ratio is {{ max_ratio }} ``
367
+
368
+ The error message if the aspect ratio of the image exceeds `maxRatio `_.
369
+
370
+ minRatioMessage
371
+ ~~~~~~~~~~~~~~~
372
+
373
+ **type **: ``string `` **default **: ``The image ratio is too small ({{ ratio }}).
374
+ Minimum ratio expected is {{ min_ratio }} ``
375
+
376
+ The error message if the aspect ratio of the image is less than `minRatio `_.
377
+
378
+ allowSquareMessage
379
+ ~~~~~~~~~~~~~~~~~~
380
+
381
+ **type **: ``string `` **default **: ``The image is square ({{ width }}x{{ height }}px).
382
+ Square images are not allowed ``
383
+
384
+ The error message if the image is square and you set `allowSquare `_ to ``false ``.
385
+
386
+ allowLandscapeMessage
387
+ ~~~~~~~~~~~~~~~~~~~~~
388
+
389
+ **type **: ``string `` **default **: ``The image is landscape oriented ({{ width }}x{{ height }}px).
390
+ Landscape oriented images are not allowed ``
391
+
392
+ The error message if the image is landscape oriented and you set `allowLandscape `_ to ``false ``.
393
+
394
+ allowPortraitMessage
395
+ ~~~~~~~~~~~~~~~~~~~~
396
+
397
+ **type **: ``string `` **default **: ``The image is portrait oriented ({{ width }}x{{ height }}px).
398
+ Portrait oriented images are not allowed ``
399
+
400
+ The error message if the image is portrait oriented and you set `allowPoirtrait `_ to ``false ``.
401
+
234
402
.. _`IANA website` : http://www.iana.org/assignments/media-types/image/index.html
0 commit comments