@@ -8,6 +8,11 @@ automatically setup to work for image files specifically.
8
8
Additionally, as of Symfony 2.1, it has options so you can validate against
9
9
the width and height 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
@@ -114,18 +131,18 @@ it is between a certain size, add the following:
114
131
.. code-block :: php
115
132
116
133
// src/Acme/BlogBundle/Entity/Author.php
117
- // ...
134
+ namespace Acme/BlogBundle/Entity
118
135
119
136
use Symfony\Component\Validator\Mapping\ClassMetadata;
120
- use Symfony\Component\Validator\Constraints\Image ;
137
+ use Symfony\Component\Validator\Constraints as Assert ;
121
138
122
139
class Author
123
140
{
124
141
// ...
125
142
126
143
public static function loadValidatorMetadata(ClassMetadata $metadata)
127
144
{
128
- $metadata->addPropertyConstraint('headshot', new Image(array(
145
+ $metadata->addPropertyConstraint('headshot', new Assert\ Image(array(
129
146
'minWidth' => 200,
130
147
'maxWidth' => 400,
131
148
'minHeight' => 200,
@@ -137,6 +154,76 @@ it is between a certain size, add the following:
137
154
The ``headshot `` property is validated to guarantee that it is a real image
138
155
and that it is between a certain width and height.
139
156
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
+
140
227
Options
141
228
-------
142
229
@@ -191,6 +278,43 @@ maxHeight
191
278
If set, the height of the image file must be less than or equal to this
192
279
value in pixels.
193
280
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
+
194
318
sizeNotDetectedMessage
195
319
~~~~~~~~~~~~~~~~~~~~~~
196
320
@@ -203,29 +327,73 @@ options has been set.
203
327
maxWidthMessage
204
328
~~~~~~~~~~~~~~~
205
329
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 ``
207
332
208
333
The error message if the width of the image exceeds `maxWidth `_.
209
334
210
335
minWidthMessage
211
336
~~~~~~~~~~~~~~~
212
337
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 ``
214
340
215
341
The error message if the width of the image is less than `minWidth `_.
216
342
217
343
maxHeightMessage
218
344
~~~~~~~~~~~~~~~~
219
345
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 ``
221
348
222
349
The error message if the height of the image exceeds `maxHeight `_.
223
350
224
351
minHeightMessage
225
352
~~~~~~~~~~~~~~~~
226
353
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 ``
228
356
229
357
The error message if the height of the image is less than `minHeight `_.
230
358
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
+
231
399
.. _`IANA website` : http://www.iana.org/assignments/media-types/image/index.html
0 commit comments