Skip to content

Commit

Permalink
[Form] Refactored code from CoreExtension to new ValidatorExtension
Browse files Browse the repository at this point in the history
CoreExtension is now independent of the Symfony2 validator.
  • Loading branch information
webmozart committed Apr 22, 2011
1 parent 1ce2db8 commit 6f1bc35
Show file tree
Hide file tree
Showing 13 changed files with 191 additions and 81 deletions.
Expand Up @@ -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>
Expand Down
12 changes: 2 additions & 10 deletions src/Symfony/Component/Form/Extension/Core/CoreExtension.php
Expand Up @@ -18,22 +18,19 @@

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(),
Expand Down Expand Up @@ -61,9 +58,4 @@ protected function loadTypes()
new Type\FileType($this->storage),
);
}

public function loadTypeGuesser()
{
return new CoreTypeGuesser($this->validator->getMetadataFactory());
}
}
19 changes: 1 addition & 18 deletions src/Symfony/Component/Form/Extension/Core/Type/FieldType.php
Expand Up @@ -19,19 +19,11 @@
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']) {
Expand All @@ -44,24 +36,17 @@ public function buildForm(FormBuilder $builder, array $options)
$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());
Expand Down Expand Up @@ -111,11 +96,9 @@ public function getDefaultOptions(array $options)
'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'])) {
Expand Down
@@ -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';
}
}
Expand Up @@ -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;
Expand Down
@@ -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());
}
}
Expand Up @@ -9,15 +9,15 @@
* 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;
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;

Expand Down
3 changes: 1 addition & 2 deletions tests/Symfony/Tests/Component/Form/AbstractLayoutTest.php
Expand Up @@ -29,12 +29,11 @@ protected function setUp()
\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),
));
}
Expand Down
Expand Up @@ -63,47 +63,6 @@ public function setReferenceCopy($reference)

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());
Expand Down
Expand Up @@ -18,8 +18,6 @@

abstract class TypeTestCase extends \PHPUnit_Framework_TestCase
{
protected $validator;

protected $storage;

protected $factory;
Expand All @@ -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()
Expand All @@ -44,7 +41,7 @@ protected function setUp()
protected function getExtensions()
{
return array(
new CoreExtension($this->validator, $this->storage),
new CoreExtension($this->storage),
);
}

Expand Down
@@ -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 comments on commit 6f1bc35

Please sign in to comment.