22namespace Barryvdh \Form \Tests ;
33
44use Barryvdh \Form \Facade \FormFactory ;
5+ use Barryvdh \Form \Tests \Types \ParentFormType ;
56use Barryvdh \Form \Tests \Types \UserFormType ;
6- use Symfony \Component \Form \Extension \Core \Type \FormType ;
7- use Symfony \Component \Form \Extension \Core \Type \TextType ;
8- use Symfony \Component \Form \Extension \Core \Type \EmailType ;
97use Symfony \Component \Form \Extension \Core \Type \SubmitType ;
108use Symfony \Component \HttpFoundation \Request ;
119
1210class ValidationTest extends TestCase
1311{
14-
1512 public function testValidForm ()
1613 {
17- /** @var UserFormType $form */
14+ /** @var \Symfony\Component\Form\Form $form */
1815 $ form = FormFactory::create (UserFormType::class, [])
1916 ->add ('save ' , SubmitType::class, ['label ' => 'Save user ' ]);
2017
@@ -34,7 +31,7 @@ public function testValidForm()
3431
3532 public function testMissingNameForm ()
3633 {
37- /** @var UserFormType $form */
34+ /** @var \Symfony\Component\Form\Form $form */
3835 $ form = FormFactory::create (UserFormType::class, [])
3936 ->add ('save ' , SubmitType::class, ['label ' => 'Save user ' ]);
4037
@@ -49,11 +46,12 @@ public function testMissingNameForm()
4946
5047 $ this ->assertTrue ($ form ->isSubmitted ());
5148 $ this ->assertFalse ($ form ->isValid ());
49+ $ this ->assertEquals ('The name field is required. ' , $ form ->getErrors (true )[0 ]->getMessage ());
5250 }
5351
5452 public function testInvalidEmailForm ()
5553 {
56- /** @var UserFormType $form */
54+ /** @var \Symfony\Component\Form\Form $form */
5755 $ form = FormFactory::create (UserFormType::class, [])
5856 ->add ('save ' , SubmitType::class, ['label ' => 'Save user ' ]);
5957
@@ -69,6 +67,86 @@ public function testInvalidEmailForm()
6967
7068 $ this ->assertTrue ($ form ->isSubmitted ());
7169 $ this ->assertFalse ($ form ->isValid ());
70+ $ this ->assertEquals ('The email must be a valid email address. ' , $ form ->getErrors (true )[0 ]->getMessage ());
71+ }
72+
73+ public function testEmptyCollectionForm ()
74+ {
75+ /** @var \Symfony\Component\Form\Form $form */
76+ $ form = FormFactory::create (ParentFormType::class, [])
77+ ->add ('save ' , SubmitType::class, ['label ' => 'Save parent ' ]);
78+
79+ $ request = $ this ->createPostRequest ([
80+ 'parent_form ' => [
81+ 'name ' => 'Barry ' ,
82+ 'save ' => true ,
83+ ]
84+ ]);
85+
86+ $ form ->handleRequest ($ request );
87+
88+ $ this ->assertTrue ($ form ->isSubmitted ());
89+ $ this ->assertFalse ($ form ->isValid ());
90+ $ this ->assertEquals ('The children field is required. ' , $ form ->getErrors (true )[0 ]->getMessage ());
91+ }
92+
93+ public function testInvalidCollectionForm ()
94+ {
95+ /** @var \Symfony\Component\Form\Form $form */
96+ $ form = FormFactory::create (ParentFormType::class, [])
97+ ->add ('save ' , SubmitType::class, ['label ' => 'Save parent ' ]);
98+
99+ $ request = $ this ->createPostRequest ([
100+ 'parent_form ' => [
101+ 'name ' => 'Barry ' ,
102+ 'children ' => [
103+ [
104+ 'name ' => 'Foo ' ,
105+ 'email ' => 'foo@example.com ' ,
106+ ],
107+ [
108+ 'name ' => 'Bar ' ,
109+ 'email ' => 'bar ' ,
110+ ]
111+ ],
112+ 'save ' => true ,
113+ ]
114+ ]);
115+
116+ $ form ->handleRequest ($ request );
117+
118+ $ this ->assertTrue ($ form ->isSubmitted ());
119+ $ this ->assertFalse ($ form ->isValid ());
120+ $ this ->assertEquals ('The children.1.email must be a valid email address. ' , $ form ->getErrors (true )[0 ]->getMessage ());
121+ }
122+
123+ public function testValidCollectionForm ()
124+ {
125+ /** @var \Symfony\Component\Form\Form $form */
126+ $ form = FormFactory::create (ParentFormType::class, [])
127+ ->add ('save ' , SubmitType::class, ['label ' => 'Save parent ' ]);
128+
129+ $ request = $ this ->createPostRequest ([
130+ 'parent_form ' => [
131+ 'name ' => 'Barry ' ,
132+ 'children ' => [
133+ [
134+ 'name ' => 'Foo ' ,
135+ 'email ' => 'foo@example.com ' ,
136+ ],
137+ [
138+ 'name ' => 'Bar ' ,
139+ 'email ' => 'bar@example.com ' ,
140+ ]
141+ ],
142+ 'save ' => true ,
143+ ]
144+ ]);
145+
146+ $ form ->handleRequest ($ request );
147+
148+ $ this ->assertTrue ($ form ->isSubmitted ());
149+ $ this ->assertTrue ($ form ->isValid ());
72150 }
73151
74152 private function createPostRequest ($ data )
0 commit comments