Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Form] Refactored code from CoreExtension to new ValidatorExtension
CoreExtension is now independent of the Symfony2 validator.
- Loading branch information
Showing
13 changed files
with
191 additions
and
81 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
52 changes: 52 additions & 0 deletions
52
src/Symfony/Component/Form/Extension/Validator/Type/FieldTypeValidatorExtension.php
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,52 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien.potencier@symfony-project.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Form\Extension\Validator\Type; | ||
|
||
use Symfony\Component\Form\AbstractTypeExtension; | ||
use Symfony\Component\Form\FormBuilder; | ||
use Symfony\Component\Form\Extension\Validator\Validator\DelegatingValidator; | ||
use Symfony\Component\Validator\ValidatorInterface; | ||
|
||
class FieldTypeValidatorExtension extends AbstractTypeExtension | ||
{ | ||
private $validator; | ||
|
||
public function __construct(ValidatorInterface $validator) | ||
{ | ||
$this->validator = $validator; | ||
} | ||
|
||
public function buildForm(FormBuilder $builder, array $options) | ||
{ | ||
$options['validation_groups'] = empty($options['validation_groups']) | ||
? null | ||
: (array)$options['validation_groups']; | ||
|
||
$builder | ||
->setAttribute('validation_groups', $options['validation_groups']) | ||
->setAttribute('validation_constraint', $options['validation_constraint']) | ||
->addValidator(new DelegatingValidator($this->validator)); | ||
} | ||
|
||
public function getDefaultOptions(array $options) | ||
{ | ||
return array( | ||
'validation_groups' => null, | ||
'validation_constraint' => null, | ||
); | ||
} | ||
|
||
public function getExtendedType() | ||
{ | ||
return 'field'; | ||
} | ||
} |
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
38 changes: 38 additions & 0 deletions
38
src/Symfony/Component/Form/Extension/Validator/ValidatorExtension.php
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,38 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien.potencier@symfony-project.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Form\Extension\Validator; | ||
|
||
use Symfony\Component\Form\Extension\Validator\Type; | ||
use Symfony\Component\Form\AbstractExtension; | ||
use Symfony\Component\Validator\ValidatorInterface; | ||
|
||
class ValidatorExtension extends AbstractExtension | ||
{ | ||
private $validator; | ||
|
||
public function __construct(ValidatorInterface $validator) | ||
{ | ||
$this->validator = $validator; | ||
} | ||
|
||
protected function loadTypeExtensions() | ||
{ | ||
return array( | ||
new Type\FieldTypeValidatorExtension($this->validator), | ||
); | ||
} | ||
|
||
public function loadTypeGuesser() | ||
{ | ||
return new ValidatorTypeGuesser($this->validator->getMetadataFactory()); | ||
} | ||
} |
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
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
56 changes: 56 additions & 0 deletions
56
...Symfony/Tests/Component/Form/Extension/Validator/Type/FieldTypeValidatorExtensionTest.php
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,56 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Tests\Component\Form\Extension\Validator\Type; | ||
|
||
class FieldTypeValidatorExtensionTest extends TypeTestCase | ||
{ | ||
public function testValidationGroupNullByDefault() | ||
{ | ||
$form = $this->factory->create('field'); | ||
|
||
$this->assertNull($form->getAttribute('validation_groups')); | ||
} | ||
|
||
public function testValidationGroupsCanBeSetToString() | ||
{ | ||
$form = $this->factory->create('field', null, array( | ||
'validation_groups' => 'group', | ||
)); | ||
|
||
$this->assertEquals(array('group'), $form->getAttribute('validation_groups')); | ||
} | ||
|
||
public function testValidationGroupsCanBeSetToArray() | ||
{ | ||
$form = $this->factory->create('field', null, array( | ||
'validation_groups' => array('group1', 'group2'), | ||
)); | ||
|
||
$this->assertEquals(array('group1', 'group2'), $form->getAttribute('validation_groups')); | ||
} | ||
|
||
public function testBindValidatesData() | ||
{ | ||
$builder = $this->factory->createBuilder('field', null, array( | ||
'validation_groups' => 'group', | ||
)); | ||
$builder->add('firstName', 'field'); | ||
$form = $builder->getForm(); | ||
|
||
$this->validator->expects($this->once()) | ||
->method('validate') | ||
->with($this->equalTo($form)); | ||
|
||
// specific data is irrelevant | ||
$form->bind(array()); | ||
} | ||
} |
Oops, something went wrong.