Skip to content

Commit 8213195

Browse files
committed
Merge branch 'pull/12053' into 2.3
* pull/12053: [Doc] Use Markdown syntax highlighting Conflicts: src/Symfony/Component/Finder/README.md
2 parents 50d51de + 2ac7d6f commit 8213195

File tree

1 file changed

+58
-52
lines changed

1 file changed

+58
-52
lines changed

README.md

Lines changed: 58 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@ The component provides "validation constraints", which are simple objects
1212
containing the rules for the validation. Let's validate a simple string
1313
as an example:
1414

15-
use Symfony\Component\Validator\Validation;
16-
use Symfony\Component\Validator\Constraints\Length;
15+
```php
16+
use Symfony\Component\Validator\Validation;
17+
use Symfony\Component\Validator\Constraints\Length;
1718

18-
$validator = Validation::createValidator();
19+
$validator = Validation::createValidator();
1920

20-
$violations = $validator->validateValue('Bernhard', new Length(array('min' => 10)));
21+
$violations = $validator->validateValue('Bernhard', new Length(array('min' => 10)));
22+
```
2123

2224
This validation will fail because the given string is shorter than ten
2325
characters. The precise errors, here called "constraint violations", are
@@ -26,24 +28,26 @@ If the violation list is empty, validation succeeded.
2628

2729
Validation of arrays is possible using the `Collection` constraint:
2830

29-
use Symfony\Component\Validator\Validation;
30-
use Symfony\Component\Validator\Constraints as Assert;
31+
```php
32+
use Symfony\Component\Validator\Validation;
33+
use Symfony\Component\Validator\Constraints as Assert;
3134

32-
$validator = Validation::createValidator();
35+
$validator = Validation::createValidator();
3336

34-
$constraint = new Assert\Collection(array(
35-
'name' => new Assert\Collection(array(
36-
'first_name' => new Assert\Length(array('min' => 101)),
37-
'last_name' => new Assert\Length(array('min' => 1)),
38-
)),
39-
'email' => new Assert\Email(),
40-
'simple' => new Assert\Length(array('min' => 102)),
41-
'gender' => new Assert\Choice(array(3, 4)),
42-
'file' => new Assert\File(),
43-
'password' => new Assert\Length(array('min' => 60)),
44-
));
37+
$constraint = new Assert\Collection(array(
38+
'name' => new Assert\Collection(array(
39+
'first_name' => new Assert\Length(array('min' => 101)),
40+
'last_name' => new Assert\Length(array('min' => 1)),
41+
)),
42+
'email' => new Assert\Email(),
43+
'simple' => new Assert\Length(array('min' => 102)),
44+
'gender' => new Assert\Choice(array(3, 4)),
45+
'file' => new Assert\File(),
46+
'password' => new Assert\Length(array('min' => 60)),
47+
));
4548

46-
$violations = $validator->validateValue($input, $constraint);
49+
$violations = $validator->validateValue($input, $constraint);
50+
```
4751

4852
Again, the validator returns the list of violations.
4953

@@ -52,45 +56,47 @@ a mapping you can put constraints onto properties and objects of classes.
5256
Whenever an object of this class is validated, its properties and
5357
method results are matched against the constraints.
5458

55-
use Symfony\Component\Validator\Validation;
56-
use Symfony\Component\Validator\Constraints as Assert;
59+
```php
60+
use Symfony\Component\Validator\Validation;
61+
use Symfony\Component\Validator\Constraints as Assert;
62+
63+
class User
64+
{
65+
/**
66+
* @Assert\Length(min = 3)
67+
* @Assert\NotBlank
68+
*/
69+
private $name;
70+
71+
/**
72+
* @Assert\Email
73+
* @Assert\NotBlank
74+
*/
75+
private $email;
76+
77+
public function __construct($name, $email)
78+
{
79+
$this->name = $name;
80+
$this->email = $email;
81+
}
5782

58-
class User
83+
/**
84+
* @Assert\True(message = "The user should have a Google Mail account")
85+
*/
86+
public function isGmailUser()
5987
{
60-
/**
61-
* @Assert\Length(min = 3)
62-
* @Assert\NotBlank
63-
*/
64-
private $name;
65-
66-
/**
67-
* @Assert\Email
68-
* @Assert\NotBlank
69-
*/
70-
private $email;
71-
72-
public function __construct($name, $email)
73-
{
74-
$this->name = $name;
75-
$this->email = $email;
76-
}
77-
78-
/**
79-
* @Assert\True(message = "The user should have a Google Mail account")
80-
*/
81-
public function isGmailUser()
82-
{
83-
return false !== strpos($this->email, '@gmail.com');
84-
}
88+
return false !== strpos($this->email, '@gmail.com');
8589
}
90+
}
8691

87-
$validator = Validation::createValidatorBuilder()
88-
->enableAnnotationMapping()
89-
->getValidator();
92+
$validator = Validation::createValidatorBuilder()
93+
->enableAnnotationMapping()
94+
->getValidator();
9095

91-
$user = new User('John Doe', '[email protected]');
96+
$user = new User('John Doe', '[email protected]');
9297

93-
$violations = $validator->validate($user);
98+
$violations = $validator->validate($user);
99+
```
94100

95101
This example uses the annotation support of Doctrine Common to
96102
map constraints to properties and methods. You can also map constraints

0 commit comments

Comments
 (0)