11<?php
22namespace Barryvdh \Form \Tests ;
33
4+ use Barryvdh \Form \Extension \Validation \ValidationListener ;
45use Barryvdh \Form \Facade \FormFactory ;
56use Barryvdh \Form \Tests \Types \ParentFormType ;
67use Barryvdh \Form \Tests \Types \UserFormType ;
78use Symfony \Component \Form \Extension \Core \Type \SubmitType ;
9+ use Symfony \Component \Form \FormInterface ;
810use Symfony \Component \HttpFoundation \Request ;
911
1012class ValidationTest extends TestCase
@@ -107,7 +109,11 @@ public function testInvalidCollectionForm()
107109 [
108110 'name ' => 'Bar ' ,
109111 'email ' => 'bar ' ,
110- ]
112+ ],
113+ ],
114+ 'emails ' => [
115+ 'foo ' ,
116+ 'bar@example.com ' ,
111117 ],
112118 'save ' => true ,
113119 ]
@@ -117,7 +123,13 @@ public function testInvalidCollectionForm()
117123
118124 $ this ->assertTrue ($ form ->isSubmitted ());
119125 $ this ->assertFalse ($ form ->isValid ());
120- $ this ->assertEquals ('The children.1.email must be a valid email address. ' , $ form ->getErrors (true )[0 ]->getMessage ());
126+
127+ $ this ->assertEqualsCanonicalizing ([
128+ 'The children.1.email must be a valid email address. ' ,
129+ 'The emails.0 must be a valid email address. ' ,
130+ ], array_map (function ($ error ) {
131+ return $ error ->getMessage ();
132+ }, iterator_to_array ($ form ->getErrors (true ))));
121133 }
122134
123135 public function testValidCollectionForm ()
@@ -139,6 +151,10 @@ public function testValidCollectionForm()
139151 'email ' => 'bar@example.com ' ,
140152 ]
141153 ],
154+ 'emails ' => [
155+ 'foo@example.com ' ,
156+ 'bar@example.com ' ,
157+ ],
142158 'save ' => true ,
143159 ]
144160 ]);
@@ -149,10 +165,81 @@ public function testValidCollectionForm()
149165 $ this ->assertTrue ($ form ->isValid ());
150166 }
151167
168+ public function testFindRules () {
169+ /** @var \Symfony\Component\Form\Form $form */
170+ $ form = FormFactory::create (ParentFormType::class, []);
171+
172+ $ request = $ this ->createPostRequest ([
173+ 'parent_form ' => [
174+ 'name ' => 'Barry ' ,
175+ 'children ' => [
176+ [
177+ 'name ' => 'Foo ' ,
178+ 'email ' => 'foo@example.com ' ,
179+ ],
180+ [
181+ 'name ' => 'Bar ' ,
182+ 'email ' => 'bar@example.com ' ,
183+ ]
184+ ],
185+ 'emails ' => [
186+ 'foo@example.com ' ,
187+ 'bar@example.com ' ,
188+ ],
189+ 'save ' => true ,
190+ ]
191+ ]);
192+
193+ $ form ->handleRequest ($ request );
194+
195+ $ validator = $ this ->app ->make (TestValidationListener::class);
196+ $ rules = $ validator ->publicFindRules ($ form );
197+
198+ $ this ->assertSame ([
199+ 'name ' => [
200+ 'required ' ,
201+ 'string ' ,
202+ ],
203+ 'children ' => [
204+ 'required ' ,
205+ 'array ' ,
206+ ],
207+ 'children.* ' => [
208+ 'required ' ,
209+ ],
210+ 'children.*.name ' => [
211+ 'required ' ,
212+ 'string ' ,
213+ ],
214+ 'children.*.email ' => [
215+ 'email ' ,
216+ 'required ' ,
217+ ],
218+ 'emails ' => [
219+ 'min:1 ' ,
220+ 'required ' ,
221+ 'array ' ,
222+ ],
223+ 'emails.* ' => [
224+ 'distinct ' ,
225+ 'required ' ,
226+ 'email ' ,
227+ ],
228+ ], $ rules );
229+ }
230+
152231 private function createPostRequest ($ data )
153232 {
154233 return new Request ([], $ data , [], [], [], [
155234 'REQUEST_METHOD ' => 'POST '
156235 ]);
157236 }
158237}
238+
239+ class TestValidationListener extends ValidationListener
240+ {
241+ public function publicFindRules (FormInterface $ parent )
242+ {
243+ return $ this ->findRules ($ parent );
244+ }
245+ }
0 commit comments