11
11
use CommerceGuys \Addressing \Country \CountryRepository ;
12
12
use CommerceGuys \Addressing \Country \CountryRepositoryInterface ;
13
13
use Daften \Bundle \AddressingBundle \Entity \AddressEmbeddable ;
14
+ use Daften \Bundle \AddressingBundle \Validator \Constraints \EmbeddedAddressFormatConstraint ;
14
15
use Doctrine \Common \Persistence \ObjectRepository ;
15
16
use Symfony \Component \EventDispatcher \EventSubscriberInterface ;
16
17
use Symfony \Component \Form \FormEvent ;
17
18
use Symfony \Component \Form \FormEvents ;
18
19
use Symfony \Component \Form \FormFactoryInterface ;
20
+ use Symfony \Component \Form \FormInterface ;
21
+ use Symfony \Component \Validator \Exception \NoSuchMetadataException ;
22
+ use Symfony \Component \Validator \Mapping \PropertyMetadataInterface ;
23
+ use Symfony \Component \Validator \Validator \ValidatorInterface ;
19
24
20
25
class AddressEmbeddableTypeSubscriber implements EventSubscriberInterface
21
26
{
@@ -39,6 +44,11 @@ class AddressEmbeddableTypeSubscriber implements EventSubscriberInterface
39
44
*/
40
45
private $ formFactory ;
41
46
47
+ /**
48
+ * @var ?ValidatorInterface
49
+ */
50
+ private $ validator ;
51
+
42
52
/**
43
53
* @param CountryRepositoryInterface $countryRepository
44
54
* @param FormFactoryInterface $factory
@@ -47,12 +57,14 @@ public function __construct(
47
57
FormFactoryInterface $ factory ,
48
58
CountryRepositoryInterface $ countryRepository ,
49
59
AddressFormatRepositoryInterface $ addressFormatRepository ,
50
- SubdivisionRepositoryInterface $ subdivisionRepository
60
+ SubdivisionRepositoryInterface $ subdivisionRepository ,
61
+ ValidatorInterface $ validator = null
51
62
) {
52
63
$ this ->formFactory = $ factory ;
53
64
$ this ->countryRepository = $ countryRepository ;
54
65
$ this ->addressFormatRepository = $ addressFormatRepository ;
55
66
$ this ->subdivisionRepository = $ subdivisionRepository ;
67
+ $ this ->validator = $ validator ;
56
68
}
57
69
58
70
/**
@@ -101,7 +113,7 @@ public function preSetData(FormEvent $event): void
101
113
],
102
114
];
103
115
}
104
- foreach (AddressFormatHelper::getGroupedFields ($ addressFormat ->getFormat ()) as $ line_index => $ line_fields ) {
116
+ foreach (AddressFormatHelper::getGroupedFields ($ addressFormat ->getFormat (), $ this -> getFieldOverrides ( $ form ) ) as $ line_index => $ line_fields ) {
105
117
foreach ($ line_fields as $ field_index => $ field ) {
106
118
$ form ->add (
107
119
$ field ,
@@ -142,7 +154,7 @@ public function preSubmit(FormEvent $event): void
142
154
$ form ->remove ($ field );
143
155
}
144
156
145
- foreach (AddressFormatHelper::getGroupedFields ($ addressFormat ->getFormat ()) as $ line_index => $ line_fields ) {
157
+ foreach (AddressFormatHelper::getGroupedFields ($ addressFormat ->getFormat (), $ this -> getFieldOverrides ( $ form ) ) as $ line_index => $ line_fields ) {
146
158
foreach ($ line_fields as $ field_index => $ field ) {
147
159
$ form ->add ($ field );
148
160
}
@@ -153,4 +165,40 @@ public function preSubmit(FormEvent $event): void
153
165
$ form ->remove ($ field );
154
166
}
155
167
}
168
+
169
+ private function getFieldOverrides (FormInterface $ form )
170
+ {
171
+ if (!$ this ->validator ) {
172
+ return null ;
173
+ }
174
+
175
+ $ formParent = $ form ->getParent ();
176
+ if (!$ formParent ) {
177
+ return null ;
178
+ }
179
+
180
+ $ parentEntity = $ formParent ->getData ();
181
+ if (!is_object ($ parentEntity )) {
182
+ return null ;
183
+ }
184
+
185
+ try {
186
+ $ metadata = $ this ->validator ->getMetadataFor (get_class ($ parentEntity ));
187
+ } catch (NoSuchMetadataException $ e ) {
188
+ return null ;
189
+ }
190
+
191
+ $ propertyMetadatas = $ metadata ->getPropertyMetadata ($ form ->getName ());
192
+ /** @var PropertyMetadataInterface $propertyMetadata */
193
+ foreach ($ propertyMetadatas as $ propertyMetadata ) {
194
+ $ constraints = $ propertyMetadata ->getConstraints ();
195
+ foreach ($ constraints as $ constraint ) {
196
+ if ($ constraint instanceof EmbeddedAddressFormatConstraint) {
197
+ return $ constraint ->fieldOverrides ;
198
+ }
199
+ }
200
+ }
201
+
202
+ return null ;
203
+ }
156
204
}
0 commit comments