You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Also, you can add custom validators by `EntityValidator::addValidator()` or create instance of [ColumnValidator](src/ColumnValidators/ColumnValidator.php) interface.
6
10
7
11
Validator skip validation:
8
12
- If `Column` attribute declared as not `updatable` and/or `insertable`
9
13
- Validation on persist/insert and property have `\Doctrine\ORM\Mapping\Id` attribute
10
14
11
-
Validator checks:
12
-
- If property value is null (or not defined), but `Column` attribute not declared as `nullable` or don't have default value (`options: ['default' => '...']`)
15
+
Validator checks by column type:
16
+
- If property value is null (or not defined), but `Column` attribute not declared as `nullable` or/and don't have default value (`options: ['default' => '...']`)
13
17
- If `Column` attribute have **numeric** type (integer, float, decimal and etc.):
14
18
- If defined `unsigned` option, then property value must be more than zero
15
19
- If type `decimal` and defined `precision`, then check size of value
@@ -19,13 +23,25 @@ Validator checks:
19
23
- If `Column` attribute have **enum** type:
20
24
- If defined `enumType`, then check is proprerty value is declared in enum class
21
25
22
-
## Example
26
+
`ColumnValidator` attributes:
27
+
-[MinLength](src/ColumnValidators/MinLength.php)
28
+
-[Greater](src/ColumnValidators/Greater.php)
29
+
-[Number](src/ColumnValidators/Number.php)
30
+
-[Regexp](src/ColumnValidators/Regexp.php)
31
+
-[Filter](src/ColumnValidators/Filter.php)
32
+
-[Slug](src/ColumnValidators/Slug.php)
33
+
-[Ip](src/ColumnValidators/Ip.php)
34
+
-[Url](src/ColumnValidators/Url.php)
35
+
-[Email](src/ColumnValidators/Email.php)
36
+
37
+
## Examples
23
38
24
39
Validates `Product` entity before insert/update data:
25
40
26
41
```php
27
42
use Doctrine\DBAL\Types\Types;
28
43
use Doctrine\ORM\Mapping as ORM;
44
+
use \EnsoStudio\Doctrine\ORM\ColumnValidators;
29
45
use EnsoStudio\Doctrine\ORM\EntityValidator;
30
46
use EnsoStudio\Doctrine\ORM\EntityValidationException;
31
47
@@ -36,19 +52,25 @@ class Product
36
52
{
37
53
...
38
54
39
-
#[ORM\Column(type: Types::STRING, length: 255)]
55
+
#[ORM\Column(type: Types::STRING, length: 200)]
56
+
#[ColumnValidators\MinLength(2)]
57
+
#[ColumnValidators\Slug]
58
+
private string $slug;
59
+
60
+
#[ORM\Column(type: Types::STRING, length: 150)]
40
61
private string $name;
41
62
42
63
#[ORM\PrePersist]
43
64
public function beforeInsert(): void
44
65
{
45
66
$validator = new EntityValidator($this);
67
+
// Callback same to ColumnValidators\MinLength(3)
46
68
$validator->addValidator(
47
69
'name',
48
-
static function (mixed $propertyValue, string $propertyName, object $entity) {
70
+
static function (string $propertyValue, string $propertyName, object $entity) {
49
71
if (mb_strlen($propertyValue) < 3) {
50
72
throw new EntityValidationException(
51
-
['% less than 3 chars', $propertyName],
73
+
['% less than 3 characters', $propertyName],
52
74
$propertyName,
53
75
$entity
54
76
);
@@ -66,4 +88,32 @@ class Product
66
88
$validator->validate(true);
67
89
}
68
90
}
91
+
```
92
+
93
+
Or you can use `EntityValidationSubscriber` to validates all entities:
94
+
95
+
```php
96
+
use Doctrine\ORM\EntityManager;
97
+
use EnsoStudio\Doctrine\ORM\EntityValidationSubscriber;
98
+
99
+
...
100
+
$entityManager = new EntityManager($connection, $config);
0 commit comments