@@ -36,7 +36,7 @@ your application::
36
36
37
37
class Author
38
38
{
39
- public $name;
39
+ private $name;
40
40
}
41
41
42
42
So far, this is just an ordinary class that serves some purpose inside your
@@ -55,6 +55,7 @@ following:
55
55
.. code-block :: php-annotations
56
56
57
57
// src/Entity/Author.php
58
+ namespace App\Entity;
58
59
59
60
// ...
60
61
use Symfony\Component\Validator\Constraints as Assert;
@@ -64,7 +65,7 @@ following:
64
65
/**
65
66
* @Assert\NotBlank
66
67
*/
67
- public $name;
68
+ private $name;
68
69
}
69
70
70
71
.. code-block :: yaml
@@ -94,14 +95,14 @@ following:
94
95
.. code-block :: php
95
96
96
97
// src/Entity/Author.php
97
-
98
+ namespace App\Entity;
98
99
// ...
99
100
use Symfony\Component\Validator\Constraints\NotBlank;
100
101
use Symfony\Component\Validator\Mapping\ClassMetadata;
101
102
102
103
class Author
103
104
{
104
- public $name;
105
+ private $name;
105
106
106
107
public static function loadValidatorMetadata(ClassMetadata $metadata)
107
108
{
@@ -111,8 +112,9 @@ following:
111
112
112
113
.. tip ::
113
114
114
- Protected and private properties can also be validated, as well as "getter"
115
- methods (see :ref: `validator-constraint-targets `).
115
+ Symfony's validator uses PHP reflection, as well as *"getter" * methods, to
116
+ get the value of any property, so they can be public, private or protected
117
+ (see :ref: `validator-constraint-targets `).
116
118
117
119
.. index ::
118
120
single: Validation; Using the validator
@@ -325,6 +327,7 @@ literature genre mostly associated with the author, which can be set to either
325
327
.. code-block :: php-annotations
326
328
327
329
// src/Entity/Author.php
330
+ namespace App\Entity;
328
331
329
332
// ...
330
333
use Symfony\Component\Validator\Constraints as Assert;
@@ -337,7 +340,7 @@ literature genre mostly associated with the author, which can be set to either
337
340
* message = "Choose a valid genre."
338
341
* )
339
342
*/
340
- public $genre;
343
+ private $genre;
341
344
342
345
// ...
343
346
}
@@ -378,14 +381,15 @@ literature genre mostly associated with the author, which can be set to either
378
381
.. code-block :: php
379
382
380
383
// src/Entity/Author.php
384
+ namespace App\Entity;
381
385
382
386
// ...
383
387
use Symfony\Component\Validator\Constraints as Assert;
384
388
use Symfony\Component\Validator\Mapping\ClassMetadata;
385
389
386
390
class Author
387
391
{
388
- public $genre;
392
+ private $genre;
389
393
390
394
// ...
391
395
@@ -412,6 +416,7 @@ options can be specified in this way.
412
416
.. code-block :: php-annotations
413
417
414
418
// src/Entity/Author.php
419
+ namespace App\Entity;
415
420
416
421
// ...
417
422
use Symfony\Component\Validator\Constraints as Assert;
@@ -421,7 +426,7 @@ options can be specified in this way.
421
426
/**
422
427
* @Assert\Choice({"fiction", "non-fiction"})
423
428
*/
424
- protected $genre;
429
+ private $genre;
425
430
426
431
// ...
427
432
}
@@ -459,14 +464,15 @@ options can be specified in this way.
459
464
.. code-block :: php
460
465
461
466
// src/Entity/Author.php
467
+ namespace App\Entity;
462
468
463
469
// ...
464
470
use Symfony\Component\Validator\Constraints as Assert;
465
471
use Symfony\Component\Validator\Mapping\ClassMetadata;
466
472
467
473
class Author
468
474
{
469
- protected $genre;
475
+ private $genre;
470
476
471
477
public static function loadValidatorMetadata(ClassMetadata $metadata)
472
478
{
@@ -579,6 +585,7 @@ class to have at least 3 characters.
579
585
.. code-block :: php
580
586
581
587
// src/Entity/Author.php
588
+ namespace App\Entity;
582
589
583
590
// ...
584
591
use Symfony\Component\Validator\Constraints as Assert;
@@ -620,6 +627,7 @@ this method must return ``true``:
620
627
.. code-block :: php-annotations
621
628
622
629
// src/Entity/Author.php
630
+ namespace App\Entity;
623
631
624
632
// ...
625
633
use Symfony\Component\Validator\Constraints as Assert;
@@ -664,6 +672,7 @@ this method must return ``true``:
664
672
.. code-block :: php
665
673
666
674
// src/Entity/Author.php
675
+ namespace App\Entity;
667
676
668
677
// ...
669
678
use Symfony\Component\Validator\Constraints as Assert;
0 commit comments