22
33namespace ACSEO \Bundle \FormJsValidationBundle \Service ;
44
5+ use Symfony \Component \Form \Extension \Core \Type \RepeatedType ;
6+
57abstract class AbstractFormJsValidation
68{
7- private $ validator ;
8- private $ translator ;
9+ protected $ validator ;
10+ protected $ translator ;
911
1012 public function __construct ($ validator , $ translator )
1113 {
@@ -20,36 +22,85 @@ public function addJsValidation($form, $validationGroup = "Default")
2022 $ metadata = $ this ->validator ->getMetadataFor ($ form ->getConfig ()->getDataClass ());
2123 $ constrainedProperties = $ metadata ->getConstrainedProperties ();
2224
23-
2425 foreach ($ form ->all () as $ field ) {
2526 $ name = $ field ->getConfig ()->getName ();
26- $ type = get_class ($ field ->getConfig ()->getType ()->getInnerType ());
27- $ options = $ field ->getConfig ()->getOptions ();
27+ $ innerType = $ field ->getConfig ()->getType ()->getInnerType ();
2828
2929 if (in_array ($ name , $ constrainedProperties )) {
30+ $ type = get_class ($ innerType );
31+ $ options = $ field ->getConfig ()->getOptions ();
3032 $ constraints = $ metadata ->getPropertyMetadata ($ name )[0 ]->getConstraints ();
33+
34+ if (!isset ($ options ["attr " ])) {
35+ $ options ["attr " ] = array ();
36+ }
37+
3138 foreach ($ constraints as $ constraint ) {
3239 if (in_array ($ validationGroup , $ constraint ->groups )) {
33- if (!isset ($ options ["attr " ])) {
34- $ options ["attr " ] = array ();
35- }
3640 $ constraintName = ((new \ReflectionClass ($ constraint ))->getShortName ());
3741 if (!isset ($ mapping [$ constraintName ])) {
3842 continue ;
39- //throw new \Exception("The constraint ". $constraintName." is not known");
4043 }
4144 $ newAttrs = call_user_func_array ($ mapping [$ constraintName ], [$ constraint , $ this ->translator ]);
4245 $ options ["attr " ] = array_merge ($ options ["attr " ], $ newAttrs );
43-
4446 }
45-
4647 }
48+ if ($ innerType instanceof RepeatedType) {
49+ $ firstOptions = isset ($ options ["first_options " ]) ? $ options ["first_options " ] : array ();
50+ $ options ["first_options " ] = $ this ->addRepeatedFieldJsValidation ($ mapping , $ constraints , $ field ->get ('first ' ), $ firstOptions , $ validationGroup );
51+ $ secondOptions = isset ($ options ["second_options " ]) ? $ options ["second_options " ] : array ();
52+ $ options ["second_options " ] = $ this ->addRepeatedFieldJsValidation ($ mapping , $ constraints , $ field ->get ('second ' ), $ secondOptions , $ validationGroup );
53+ }
54+
4755 $ form ->add ($ name , $ type , $ options );
4856 }
4957 }
5058
5159 return $ form ;
5260 }
5361
62+ private function addRepeatedFieldJsValidation ($ mapping , $ constraints , $ field , $ options , $ validationGroup = "Default " )
63+ {
64+ $ options = $ field ->getConfig ()->getOptions ();
65+
66+ if (!isset ($ options ["attr " ])) {
67+ $ options ["attr " ] = array ();
68+ }
69+
70+ foreach ($ constraints as $ constraint ) {
71+ if (in_array ($ validationGroup , $ constraint ->groups )) {
72+ $ constraintName = ((new \ReflectionClass ($ constraint ))->getShortName ());
73+ if (!isset ($ mapping [$ constraintName ])) {
74+ continue ;
75+ }
76+ $ newAttrs = call_user_func_array ($ mapping [$ constraintName ], [$ constraint , $ this ->translator ]);
77+ $ options ["attr " ] = array_merge ($ options ["attr " ], $ newAttrs );
78+ }
79+ }
80+
81+ // Add a constraint to the second field in order to control the equality with the first one
82+ if ($ field ->getConfig ()->getName () == 'second ' ) {
83+ $ options ["attr " ] = $ this ->addEqualToConstraint ($ field , $ options ["attr " ]);
84+ }
85+
86+ return $ options ;
87+ }
88+
89+ protected function getFieldId ($ field )
90+ {
91+ $ completeNameArray = [$ field ->getConfig ()->getName ()];
92+ $ parent = $ field ;
93+ while ($ parent = $ parent ->getParent ()) {
94+ $ completeNameArray [] = $ parent ->getConfig ()->getName ();
95+ }
96+
97+ return implode ('_ ' , array_reverse ($ completeNameArray ));
98+ }
99+
100+ protected function addEqualToConstraint ($ field , $ attrOptions )
101+ {
102+ return $ attrOptions ;
103+ }
104+
54105 abstract protected function getMapping ();
55106}
0 commit comments