1
1
<?php
2
2
namespace Barryvdh \Form \Tests ;
3
3
4
+ use Barryvdh \Form \Extension \Validation \ValidationListener ;
4
5
use Barryvdh \Form \Facade \FormFactory ;
5
6
use Barryvdh \Form \Tests \Types \ParentFormType ;
6
7
use Barryvdh \Form \Tests \Types \UserFormType ;
7
8
use Symfony \Component \Form \Extension \Core \Type \SubmitType ;
9
+ use Symfony \Component \Form \FormInterface ;
8
10
use Symfony \Component \HttpFoundation \Request ;
9
11
10
12
class ValidationTest extends TestCase
@@ -107,7 +109,11 @@ public function testInvalidCollectionForm()
107
109
[
108
110
'name ' => 'Bar ' ,
109
111
'email ' => 'bar ' ,
110
- ]
112
+ ],
113
+ ],
114
+ 'emails ' => [
115
+ 'foo ' ,
116
+
111
117
],
112
118
'save ' => true ,
113
119
]
@@ -117,7 +123,13 @@ public function testInvalidCollectionForm()
117
123
118
124
$ this ->assertTrue ($ form ->isSubmitted ());
119
125
$ 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 ))));
121
133
}
122
134
123
135
public function testValidCollectionForm ()
@@ -139,6 +151,10 @@ public function testValidCollectionForm()
139
151
140
152
]
141
153
],
154
+ 'emails ' => [
155
+
156
+
157
+ ],
142
158
'save ' => true ,
143
159
]
144
160
]);
@@ -149,10 +165,81 @@ public function testValidCollectionForm()
149
165
$ this ->assertTrue ($ form ->isValid ());
150
166
}
151
167
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
+
179
+ ],
180
+ [
181
+ 'name ' => 'Bar ' ,
182
+
183
+ ]
184
+ ],
185
+ 'emails ' => [
186
+
187
+
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
+
152
231
private function createPostRequest ($ data )
153
232
{
154
233
return new Request ([], $ data , [], [], [], [
155
234
'REQUEST_METHOD ' => 'POST '
156
235
]);
157
236
}
158
237
}
238
+
239
+ class TestValidationListener extends ValidationListener
240
+ {
241
+ public function publicFindRules (FormInterface $ parent )
242
+ {
243
+ return $ this ->findRules ($ parent );
244
+ }
245
+ }
0 commit comments