[Form] Refactored code from CoreExtension to new ValidatorExtension
CoreExtension is now independent of the Symfony2 validator.
This commit is contained in:
parent
1ce2db87e2
commit
6f1bc356a8
@ -7,7 +7,7 @@
|
||||
<parameters>
|
||||
<parameter key="form.extension.class">Symfony\Component\Form\Extension\DependencyInjection\DependencyInjectionExtension</parameter>
|
||||
<parameter key="form.factory.class">Symfony\Component\Form\FormFactory</parameter>
|
||||
<parameter key="form.type_guesser.validator.class">Symfony\Component\Form\Extension\Core\CoreTypeGuesser</parameter>
|
||||
<parameter key="form.type_guesser.validator.class">Symfony\Component\Form\Extension\Validator\ValidatorTypeGuesser</parameter>
|
||||
<parameter key="form.csrf_provider.class">Symfony\Component\Form\Extension\Csrf\CsrfProvider\SessionCsrfProvider</parameter>
|
||||
<parameter key="form.csrf_protection.enabled">true</parameter>
|
||||
<parameter key="form.csrf_protection.field_name">_token</parameter>
|
||||
|
@ -18,22 +18,19 @@ use Symfony\Component\HttpFoundation\File\TemporaryStorage;
|
||||
|
||||
class CoreExtension extends AbstractExtension
|
||||
{
|
||||
private $validator;
|
||||
|
||||
private $storage;
|
||||
|
||||
private $typeGuesser;
|
||||
|
||||
public function __construct(ValidatorInterface $validator, TemporaryStorage $storage)
|
||||
public function __construct(TemporaryStorage $storage)
|
||||
{
|
||||
$this->validator = $validator;
|
||||
$this->storage = $storage;
|
||||
}
|
||||
|
||||
protected function loadTypes()
|
||||
{
|
||||
return array(
|
||||
new Type\FieldType($this->validator),
|
||||
new Type\FieldType(),
|
||||
new Type\FormType(),
|
||||
new Type\BirthdayType(),
|
||||
new Type\CheckboxType(),
|
||||
@ -61,9 +58,4 @@ class CoreExtension extends AbstractExtension
|
||||
new Type\FileType($this->storage),
|
||||
);
|
||||
}
|
||||
|
||||
public function loadTypeGuesser()
|
||||
{
|
||||
return new CoreTypeGuesser($this->validator->getMetadataFactory());
|
||||
}
|
||||
}
|
||||
|
@ -19,19 +19,11 @@ use Symfony\Component\Form\FormFactoryInterface;
|
||||
use Symfony\Component\Form\FormView;
|
||||
use Symfony\Component\Form\Extension\Core\EventListener\TrimListener;
|
||||
use Symfony\Component\Form\Extension\Core\Validator\DefaultValidator;
|
||||
use Symfony\Component\Form\Extension\Core\Validator\DelegatingValidator;
|
||||
use Symfony\Component\EventDispatcher\EventDispatcher;
|
||||
use Symfony\Component\Validator\ValidatorInterface;
|
||||
|
||||
class FieldType extends AbstractType
|
||||
{
|
||||
private $validator;
|
||||
|
||||
public function __construct(ValidatorInterface $validator)
|
||||
{
|
||||
$this->validator = $validator;
|
||||
}
|
||||
|
||||
public function buildForm(FormBuilder $builder, array $options)
|
||||
{
|
||||
if (null === $options['property_path']) {
|
||||
@ -44,24 +36,17 @@ class FieldType extends AbstractType
|
||||
$options['property_path'] = new PropertyPath($options['property_path']);
|
||||
}
|
||||
|
||||
$options['validation_groups'] = empty($options['validation_groups'])
|
||||
? null
|
||||
: (array)$options['validation_groups'];
|
||||
|
||||
$builder->setRequired($options['required'])
|
||||
->setReadOnly($options['read_only'])
|
||||
->setErrorBubbling($options['error_bubbling'])
|
||||
->setEmptyData($options['empty_data'])
|
||||
->setAttribute('by_reference', $options['by_reference'])
|
||||
->setAttribute('property_path', $options['property_path'])
|
||||
->setAttribute('validation_groups', $options['validation_groups'])
|
||||
->setAttribute('error_mapping', $options['error_mapping'])
|
||||
->setAttribute('max_length', $options['max_length'])
|
||||
->setAttribute('label', $options['label'] ?: $this->humanize($builder->getName()))
|
||||
->setAttribute('validation_constraint', $options['validation_constraint'])
|
||||
->setData($options['data'])
|
||||
->addValidator(new DefaultValidator())
|
||||
->addValidator(new DelegatingValidator($this->validator));
|
||||
->addValidator(new DefaultValidator());
|
||||
|
||||
if ($options['trim']) {
|
||||
$builder->addEventSubscriber(new TrimListener());
|
||||
@ -111,11 +96,9 @@ class FieldType extends AbstractType
|
||||
'max_length' => null,
|
||||
'property_path' => null,
|
||||
'by_reference' => true,
|
||||
'validation_groups' => null,
|
||||
'error_bubbling' => false,
|
||||
'error_mapping' => array(),
|
||||
'label' => null,
|
||||
'validation_constraint' => null,
|
||||
);
|
||||
|
||||
if (!empty($options['data_class'])) {
|
||||
|
@ -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';
|
||||
}
|
||||
}
|
@ -9,7 +9,7 @@
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\Form\Extension\Core\Validator;
|
||||
namespace Symfony\Component\Form\Extension\Validator\Validator;
|
||||
|
||||
use Symfony\Component\Form\FormInterface;
|
||||
use Symfony\Component\Form\FormValidatorInterface;
|
@ -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());
|
||||
}
|
||||
}
|
@ -9,7 +9,7 @@
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\Form\Extension\Core;
|
||||
namespace Symfony\Component\Form\Extension\Validator;
|
||||
|
||||
use Symfony\Component\Form\FormTypeGuesserInterface;
|
||||
use Symfony\Component\Form\Guess\Guess;
|
||||
@ -17,7 +17,7 @@ use Symfony\Component\Form\Guess\TypeGuess;
|
||||
use Symfony\Component\Form\Guess\ValueGuess;
|
||||
use Symfony\Component\Validator\Mapping\ClassMetadataFactoryInterface;
|
||||
|
||||
class CoreTypeGuesser implements FormTypeGuesserInterface
|
||||
class ValidatorTypeGuesser implements FormTypeGuesserInterface
|
||||
{
|
||||
private $metadataFactory;
|
||||
|
@ -29,12 +29,11 @@ abstract class AbstractLayoutTest extends \PHPUnit_Framework_TestCase
|
||||
\Locale::setDefault('en');
|
||||
|
||||
$dispatcher = new EventDispatcher();
|
||||
$validator = $this->getMock('Symfony\Component\Validator\ValidatorInterface');
|
||||
$this->csrfProvider = $this->getMock('Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderInterface');
|
||||
$storage = new \Symfony\Component\HttpFoundation\File\TemporaryStorage('foo', 1, \sys_get_temp_dir());
|
||||
|
||||
$this->factory = new FormFactory(array(
|
||||
new CoreExtension($validator, $storage),
|
||||
new CoreExtension($storage),
|
||||
new CsrfExtension($this->csrfProvider),
|
||||
));
|
||||
}
|
||||
|
@ -63,47 +63,6 @@ class FormTest_AuthorWithoutRefSetter
|
||||
|
||||
class FormTypeTest extends TypeTestCase
|
||||
{
|
||||
public function testValidationGroupNullByDefault()
|
||||
{
|
||||
$form = $this->factory->create('form');
|
||||
|
||||
$this->assertNull($form->getAttribute('validation_groups'));
|
||||
}
|
||||
|
||||
public function testValidationGroupsCanBeSetToString()
|
||||
{
|
||||
$form = $this->factory->create('form', null, array(
|
||||
'validation_groups' => 'group',
|
||||
));
|
||||
|
||||
$this->assertEquals(array('group'), $form->getAttribute('validation_groups'));
|
||||
}
|
||||
|
||||
public function testValidationGroupsCanBeSetToArray()
|
||||
{
|
||||
$form = $this->factory->create('form', null, array(
|
||||
'validation_groups' => array('group1', 'group2'),
|
||||
));
|
||||
|
||||
$this->assertEquals(array('group1', 'group2'), $form->getAttribute('validation_groups'));
|
||||
}
|
||||
|
||||
public function testBindValidatesData()
|
||||
{
|
||||
$builder = $this->factory->createBuilder('form', 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());
|
||||
}
|
||||
|
||||
public function testSubformDoesntCallSetters()
|
||||
{
|
||||
$author = new FormTest_AuthorWithoutRefSetter(new Author());
|
||||
|
@ -18,8 +18,6 @@ use Symfony\Component\EventDispatcher\EventDispatcher;
|
||||
|
||||
abstract class TypeTestCase extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
protected $validator;
|
||||
|
||||
protected $storage;
|
||||
|
||||
protected $factory;
|
||||
@ -32,7 +30,6 @@ abstract class TypeTestCase extends \PHPUnit_Framework_TestCase
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
$this->validator = $this->getMock('Symfony\Component\Validator\ValidatorInterface');
|
||||
$this->dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
|
||||
$this->storage = $this->getMockBuilder('Symfony\Component\HttpFoundation\File\TemporaryStorage')
|
||||
->disableOriginalConstructor()
|
||||
@ -44,7 +41,7 @@ abstract class TypeTestCase extends \PHPUnit_Framework_TestCase
|
||||
protected function getExtensions()
|
||||
{
|
||||
return array(
|
||||
new CoreExtension($this->validator, $this->storage),
|
||||
new CoreExtension($this->storage),
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -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());
|
||||
}
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
<?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\Tests\Component\Form\Extension\Validator\Type;
|
||||
|
||||
use Symfony\Tests\Component\Form\Extension\Core\Type\TypeTestCase as BaseTestCase;
|
||||
use Symfony\Component\Form\Extension\Validator\ValidatorExtension;
|
||||
|
||||
abstract class TypeTestCase extends BaseTestCase
|
||||
{
|
||||
protected $validator;
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
$this->validator = $this->getMock('Symfony\Component\Validator\ValidatorInterface');
|
||||
|
||||
parent::setUp();
|
||||
}
|
||||
|
||||
protected function getExtensions()
|
||||
{
|
||||
return array_merge(parent::getExtensions(), array(
|
||||
new ValidatorExtension($this->validator),
|
||||
));
|
||||
}
|
||||
}
|
@ -9,12 +9,12 @@
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Tests\Component\Form\Extension\Core\Validator;
|
||||
namespace Symfony\Tests\Component\Form\Extension\Validator\Validator;
|
||||
|
||||
use Symfony\Component\Form\FormBuilder;
|
||||
use Symfony\Component\Form\FormError;
|
||||
use Symfony\Component\Form\Util\PropertyPath;
|
||||
use Symfony\Component\Form\Extension\Core\Validator\DelegatingValidator;
|
||||
use Symfony\Component\Form\Extension\Validator\Validator\DelegatingValidator;
|
||||
use Symfony\Component\Form\Exception\TransformationFailedException;
|
||||
use Symfony\Component\Validator\ConstraintViolation;
|
||||
use Symfony\Component\Validator\ExecutionContext;
|
Reference in New Issue
Block a user