-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
108 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
namespace Barryvdh\Form\Tests\Types; | ||
|
||
use Symfony\Component\Form\AbstractType; | ||
use Symfony\Component\Form\Extension\Core\Type\CollectionType; | ||
use Symfony\Component\Form\Extension\Core\Type\TextType; | ||
use Symfony\Component\Form\FormBuilderInterface; | ||
|
||
class ParentFormType extends AbstractType | ||
{ | ||
public function buildForm(FormBuilderInterface $builder, array $options) | ||
{ | ||
$builder | ||
->add('name', TextType::class, [ | ||
'required' => true, | ||
]) | ||
->add('children', CollectionType::class, [ | ||
'entry_type' => UserFormType::class, | ||
'allow_add' => true, | ||
]) | ||
; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,19 +2,16 @@ | |
namespace Barryvdh\Form\Tests; | ||
|
||
use Barryvdh\Form\Facade\FormFactory; | ||
use Barryvdh\Form\Tests\Types\ParentFormType; | ||
use Barryvdh\Form\Tests\Types\UserFormType; | ||
use Symfony\Component\Form\Extension\Core\Type\FormType; | ||
use Symfony\Component\Form\Extension\Core\Type\TextType; | ||
use Symfony\Component\Form\Extension\Core\Type\EmailType; | ||
use Symfony\Component\Form\Extension\Core\Type\SubmitType; | ||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
class ValidationTest extends TestCase | ||
{ | ||
|
||
public function testValidForm() | ||
{ | ||
/** @var UserFormType $form */ | ||
/** @var \Symfony\Component\Form\Form $form */ | ||
$form = FormFactory::create(UserFormType::class, []) | ||
->add('save', SubmitType::class, ['label' => 'Save user']); | ||
|
||
|
@@ -34,7 +31,7 @@ public function testValidForm() | |
|
||
public function testMissingNameForm() | ||
{ | ||
/** @var UserFormType $form */ | ||
/** @var \Symfony\Component\Form\Form $form */ | ||
$form = FormFactory::create(UserFormType::class, []) | ||
->add('save', SubmitType::class, ['label' => 'Save user']); | ||
|
||
|
@@ -49,11 +46,12 @@ public function testMissingNameForm() | |
|
||
$this->assertTrue($form->isSubmitted()); | ||
$this->assertFalse($form->isValid()); | ||
$this->assertEquals('The name field is required.', $form->getErrors(true)[0]->getMessage()); | ||
} | ||
|
||
public function testInvalidEmailForm() | ||
{ | ||
/** @var UserFormType $form */ | ||
/** @var \Symfony\Component\Form\Form $form */ | ||
$form = FormFactory::create(UserFormType::class, []) | ||
->add('save', SubmitType::class, ['label' => 'Save user']); | ||
|
||
|
@@ -69,6 +67,86 @@ public function testInvalidEmailForm() | |
|
||
$this->assertTrue($form->isSubmitted()); | ||
$this->assertFalse($form->isValid()); | ||
$this->assertEquals('The email must be a valid email address.', $form->getErrors(true)[0]->getMessage()); | ||
} | ||
|
||
public function testEmptyCollectionForm() | ||
{ | ||
/** @var \Symfony\Component\Form\Form $form */ | ||
$form = FormFactory::create(ParentFormType::class, []) | ||
->add('save', SubmitType::class, ['label' => 'Save parent']); | ||
|
||
$request = $this->createPostRequest([ | ||
'parent_form' => [ | ||
'name' => 'Barry', | ||
'save' => true, | ||
] | ||
]); | ||
|
||
$form->handleRequest($request); | ||
|
||
$this->assertTrue($form->isSubmitted()); | ||
$this->assertFalse($form->isValid()); | ||
$this->assertEquals('The children field is required.', $form->getErrors(true)[0]->getMessage()); | ||
} | ||
|
||
public function testInvalidCollectionForm() | ||
{ | ||
/** @var \Symfony\Component\Form\Form $form */ | ||
$form = FormFactory::create(ParentFormType::class, []) | ||
->add('save', SubmitType::class, ['label' => 'Save parent']); | ||
|
||
$request = $this->createPostRequest([ | ||
'parent_form' => [ | ||
'name' => 'Barry', | ||
'children' => [ | ||
[ | ||
'name' => 'Foo', | ||
'email' => '[email protected]', | ||
], | ||
[ | ||
'name' => 'Bar', | ||
'email' => 'bar', | ||
] | ||
], | ||
'save' => true, | ||
] | ||
]); | ||
|
||
$form->handleRequest($request); | ||
|
||
$this->assertTrue($form->isSubmitted()); | ||
$this->assertFalse($form->isValid()); | ||
$this->assertEquals('The children.1.email must be a valid email address.', $form->getErrors(true)[0]->getMessage()); | ||
} | ||
|
||
public function testValidCollectionForm() | ||
{ | ||
/** @var \Symfony\Component\Form\Form $form */ | ||
$form = FormFactory::create(ParentFormType::class, []) | ||
->add('save', SubmitType::class, ['label' => 'Save parent']); | ||
|
||
$request = $this->createPostRequest([ | ||
'parent_form' => [ | ||
'name' => 'Barry', | ||
'children' => [ | ||
[ | ||
'name' => 'Foo', | ||
'email' => '[email protected]', | ||
], | ||
[ | ||
'name' => 'Bar', | ||
'email' => '[email protected]', | ||
] | ||
], | ||
'save' => true, | ||
] | ||
]); | ||
|
||
$form->handleRequest($request); | ||
|
||
$this->assertTrue($form->isSubmitted()); | ||
$this->assertTrue($form->isValid()); | ||
} | ||
|
||
private function createPostRequest($data) | ||
|