-
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.
Merge pull request #48 from bonroyage/collection-validation
Support validation for CollectionType with fields
- Loading branch information
Showing
3 changed files
with
111 additions
and
2 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
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
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 |
---|---|---|
@@ -1,10 +1,12 @@ | ||
<?php | ||
namespace Barryvdh\Form\Tests; | ||
|
||
use Barryvdh\Form\Extension\Validation\ValidationListener; | ||
use Barryvdh\Form\Facade\FormFactory; | ||
use Barryvdh\Form\Tests\Types\ParentFormType; | ||
use Barryvdh\Form\Tests\Types\UserFormType; | ||
use Symfony\Component\Form\Extension\Core\Type\SubmitType; | ||
use Symfony\Component\Form\FormInterface; | ||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
class ValidationTest extends TestCase | ||
|
@@ -107,7 +109,11 @@ public function testInvalidCollectionForm() | |
[ | ||
'name' => 'Bar', | ||
'email' => 'bar', | ||
] | ||
], | ||
], | ||
'emails' => [ | ||
'foo', | ||
'[email protected]', | ||
], | ||
'save' => true, | ||
] | ||
|
@@ -117,7 +123,13 @@ public function testInvalidCollectionForm() | |
|
||
$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()); | ||
|
||
$this->assertEqualsCanonicalizing([ | ||
'The children.1.email must be a valid email address.', | ||
'The emails.0 must be a valid email address.', | ||
], array_map(function($error) { | ||
return $error->getMessage(); | ||
}, iterator_to_array($form->getErrors(true)))); | ||
} | ||
|
||
public function testValidCollectionForm() | ||
|
@@ -139,6 +151,10 @@ public function testValidCollectionForm() | |
'email' => '[email protected]', | ||
] | ||
], | ||
'emails' => [ | ||
'[email protected]', | ||
'[email protected]', | ||
], | ||
'save' => true, | ||
] | ||
]); | ||
|
@@ -149,10 +165,81 @@ public function testValidCollectionForm() | |
$this->assertTrue($form->isValid()); | ||
} | ||
|
||
public function testFindRules() { | ||
/** @var \Symfony\Component\Form\Form $form */ | ||
$form = FormFactory::create(ParentFormType::class, []); | ||
|
||
$request = $this->createPostRequest([ | ||
'parent_form' => [ | ||
'name' => 'Barry', | ||
'children' => [ | ||
[ | ||
'name' => 'Foo', | ||
'email' => '[email protected]', | ||
], | ||
[ | ||
'name' => 'Bar', | ||
'email' => '[email protected]', | ||
] | ||
], | ||
'emails' => [ | ||
'[email protected]', | ||
'[email protected]', | ||
], | ||
'save' => true, | ||
] | ||
]); | ||
|
||
$form->handleRequest($request); | ||
|
||
$validator = $this->app->make(TestValidationListener::class); | ||
$rules = $validator->publicFindRules($form); | ||
|
||
$this->assertSame([ | ||
'name' => [ | ||
'required', | ||
'string', | ||
], | ||
'children' => [ | ||
'required', | ||
'array', | ||
], | ||
'children.*' => [ | ||
'required', | ||
], | ||
'children.*.name' => [ | ||
'required', | ||
'string', | ||
], | ||
'children.*.email' => [ | ||
'email', | ||
'required', | ||
], | ||
'emails' => [ | ||
'min:1', | ||
'required', | ||
'array', | ||
], | ||
'emails.*' => [ | ||
'distinct', | ||
'required', | ||
'email', | ||
], | ||
], $rules); | ||
} | ||
|
||
private function createPostRequest($data) | ||
{ | ||
return new Request([], $data, [], [], [], [ | ||
'REQUEST_METHOD' => 'POST' | ||
]); | ||
} | ||
} | ||
|
||
class TestValidationListener extends ValidationListener | ||
{ | ||
public function publicFindRules(FormInterface $parent) | ||
{ | ||
return $this->findRules($parent); | ||
} | ||
} |