Skip to content

Commit 11e6dca

Browse files
committed
Test collection validation #13
1 parent 61fe2ed commit 11e6dca

File tree

2 files changed

+108
-7
lines changed

2 files changed

+108
-7
lines changed

tests/Types/ParentFormType.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
namespace Barryvdh\Form\Tests\Types;
3+
4+
use Symfony\Component\Form\AbstractType;
5+
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
6+
use Symfony\Component\Form\Extension\Core\Type\TextType;
7+
use Symfony\Component\Form\FormBuilderInterface;
8+
9+
class ParentFormType extends AbstractType
10+
{
11+
public function buildForm(FormBuilderInterface $builder, array $options)
12+
{
13+
$builder
14+
->add('name', TextType::class, [
15+
'required' => true,
16+
])
17+
->add('children', CollectionType::class, [
18+
'entry_type' => UserFormType::class,
19+
'allow_add' => true,
20+
])
21+
;
22+
}
23+
}

tests/ValidationTest.php

Lines changed: 85 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,16 @@
22
namespace Barryvdh\Form\Tests;
33

44
use Barryvdh\Form\Facade\FormFactory;
5+
use Barryvdh\Form\Tests\Types\ParentFormType;
56
use 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;
97
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
108
use Symfony\Component\HttpFoundation\Request;
119

1210
class 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' => '[email protected]',
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' => '[email protected]',
136+
],
137+
[
138+
'name' => 'Bar',
139+
'email' => '[email protected]',
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

Comments
 (0)