17
17
use Symfony \Component \Validator \Constraints \NotBlank ;
18
18
use Symfony \Component \Validator \Constraints \NotNull ;
19
19
use Symfony \Component \Validator \Mapping \Factory \MetadataFactoryInterface as ValidatorMetadataFactoryInterface ;
20
+ use Symfony \Component \Validator \Mapping \PropertyMetadataInterface as ValidatorPropertyMetadataInterface ;
20
21
21
22
/**
22
23
* Decorates a metadata loader using the validator.
@@ -39,24 +40,6 @@ public function __construct(ValidatorMetadataFactoryInterface $validatorMetadata
39
40
$ this ->decorated = $ decorated ;
40
41
}
41
42
42
- /**
43
- * Is this constraint making the related property required?
44
- *
45
- * @param Constraint $constraint
46
- *
47
- * @return bool
48
- */
49
- private function isRequired (Constraint $ constraint ) : bool
50
- {
51
- foreach (self ::REQUIRED_CONSTRAINTS as $ requiredConstraint ) {
52
- if ($ constraint instanceof $ requiredConstraint ) {
53
- return true ;
54
- }
55
- }
56
-
57
- return false ;
58
- }
59
-
60
43
/**
61
44
* {@inheritdoc}
62
45
*/
@@ -68,22 +51,9 @@ public function create(string $resourceClass, string $name, array $options = [])
68
51
}
69
52
70
53
$ validatorClassMetadata = $ this ->validatorMetadataFactory ->getMetadataFor ($ resourceClass );
71
-
72
54
foreach ($ validatorClassMetadata ->getPropertyMetadata ($ name ) as $ validatorPropertyMetadata ) {
73
55
if (isset ($ options ['validation_groups ' ])) {
74
- foreach ($ options ['validation_groups ' ] as $ validationGroup ) {
75
- if (!is_string ($ validationGroup )) {
76
- continue ;
77
- }
78
-
79
- foreach ($ validatorPropertyMetadata ->findConstraints ($ validationGroup ) as $ constraint ) {
80
- if ($ this ->isRequired ($ constraint )) {
81
- return $ propertyMetadata ->withRequired (true );
82
- }
83
- }
84
- }
85
-
86
- return $ propertyMetadata ->withRequired (false );
56
+ return $ propertyMetadata ->withRequired ($ this ->isRequiredByGroups ($ validatorPropertyMetadata , $ options ));
87
57
}
88
58
89
59
foreach ($ validatorPropertyMetadata ->findConstraints ($ validatorClassMetadata ->getDefaultGroup ()) as $ constraint ) {
@@ -97,4 +67,47 @@ public function create(string $resourceClass, string $name, array $options = [])
97
67
98
68
return $ propertyMetadata ->withRequired (false );
99
69
}
70
+
71
+ /**
72
+ * Tests if the property is required because of its validation groups.
73
+ *
74
+ * @param ValidatorPropertyMetadataInterface $validatorPropertyMetadata
75
+ * @param array $options
76
+ *
77
+ * @return bool
78
+ */
79
+ private function isRequiredByGroups (ValidatorPropertyMetadataInterface $ validatorPropertyMetadata , array $ options ) : bool
80
+ {
81
+ foreach ($ options ['validation_groups ' ] as $ validationGroup ) {
82
+ if (!is_string ($ validationGroup )) {
83
+ continue ;
84
+ }
85
+
86
+ foreach ($ validatorPropertyMetadata ->findConstraints ($ validationGroup ) as $ constraint ) {
87
+ if ($ this ->isRequired ($ constraint )) {
88
+ return true ;
89
+ }
90
+ }
91
+ }
92
+
93
+ return false ;
94
+ }
95
+
96
+ /**
97
+ * Is this constraint making the related property required?
98
+ *
99
+ * @param Constraint $constraint
100
+ *
101
+ * @return bool
102
+ */
103
+ private function isRequired (Constraint $ constraint ) : bool
104
+ {
105
+ foreach (self ::REQUIRED_CONSTRAINTS as $ requiredConstraint ) {
106
+ if ($ constraint instanceof $ requiredConstraint ) {
107
+ return true ;
108
+ }
109
+ }
110
+
111
+ return false ;
112
+ }
100
113
}
0 commit comments