merged branch bschussek/bootstrap (PR #5112)

Commits
-------

b982883 [Form] Moved FormHelper back to FrameworkBundle
cb62d05 [Form] [Validator] Fixed issues mentioned in the PR
2185ca8 [Validator] Added entry point "Validation" for more convenient usage outside of Symfony2
ed87361 [Form] Moved FormHelper creation to TemplatingExtension
87ccb6a [Form] Added entry point "Forms" for more convenient usage outside of Symfony

Discussion
----------

[Form] [Validator] Added more convenient entry points for stand-alone usage

Bug fix: no
Feature addition: no
Backwards compatibility break: no
Symfony2 tests pass: yes
Fixes the following tickets: -
Todo: -

This PR greatly simplifies the usage of the Form and Validator component when used outside of Symfony2. Check out the below code to get an idea about the simplified usage:

```php
<?php

use Symfony\Component\Validator\Validation;
use Symfony\Component\Validator\Mapping\Cache\ApcCache;
use Symfony\Component\Form\Forms;
use Symfony\Component\Form\Extension\HttpFoundation\HttpFoundationExtension;
use Symfony\Component\Form\Extension\Csrf\CsrfExtension;
use Symfony\Component\Form\Extension\Csrf\CsrfProvider\SessionCsrfProvider;
use Symfony\Component\Form\Extension\Templating\TemplatingExtension;
use Symfony\Component\Form\Extension\Validator\ValidatorExtension;
use Symfony\Component\HttpFoundation\Session;
use Symfony\Component\Templating\PhpEngine;

$session = new Session();
$secret = 'V8a5Z97e...';
$csrfProvider = new SessionCsrfProvider($session, $secret);
$engine = new PhpEngine(/* ... snap ... */);

$validator = Validation::createValidator();
// or
$validator = Validation::createValidatorBuilder()
    ->addXmlMapping('path/to/mapping.xml')
    ->addYamlMapping('path/to/mapping.yml')
    ->addMethodMapping('loadValidatorMetadata')
    ->enableAnnotationMapping()
    ->setMetadataCache(new ApcCache())
    ->getValidator();

$formFactory = Forms::createFormFactory();
// or
$formFactory = Forms::createFormFactoryBuilder()
    // custom types, if you're too lazy to create an extension :)
    ->addType(new PersonType())
    ->addType(new PhoneNumberType())
    ->addTypeExtension(new FormTypeHelpTextExtension())
    // desired extensions (CoreExtension is loaded by default)
    ->addExtension(new HttpFoundationExtension())
    ->addExtension(new CsrfExtension($csrfProvider))
    ->addExtension(new TemplatingExtension($engine, $csrfProvider, array(
        'FormBundle:Form'
    ))
    ->addExtension(new ValidatorExtension($validator))
    ->getFormFactory();

$form = $formFactory->createBuilder()
    ->add('firstName', 'text')
    ->add('lastName', 'text')
    ->add('age', 'integer')
    ->add('gender', 'choice', array(
        'choices' => array('m' => 'Male', 'f' => 'Female'),
    ))
    ->getForm();

if (isset($_POST[$form->getName()])) {
    $form->bind($_POST[$form->getName()]);

    if ($form->isValid()) {
        // do stuff
    }
}

return $engine->render('AcmeHelloBundle:Hello:index.html.php', array(
    'form' => $form->createView(),
));
```

---------------------------------------------------------------------------

by bschussek at 2012-07-30T10:08:42Z

I should maybe add a comment about the benefits of this change, in case they are not self-explanatory:

* class construction with default configuration is now a one-liner
* userland code is decoupled from core implementations → userland code doesn't break if we change constructor signatures
* easier to understand, since many core classes are now created internally
* easy to discover the possible settings → just look at (FormFactory|Validator)BuilderInterface
* usage of custom interface implementations is supported, just like before

---------------------------------------------------------------------------

by fabpot at 2012-07-31T08:18:53Z

The new syntax is great.

I have one comment though about this PR about support of PHP as a templating system (support for Twig is provided by the bridge and it was already easy to configure Twig as a templating system for forms -- see Silex for instance).

The `FormHelper` has been moved into the Form component. This helper is only useful when using the PHP templating system (which is not what we recommend people to use), but the default templates are still in the Framework bundle. So using the Form component as standalone with PHP as a templating system still requires to install the bundle to get access to the default templates. Am I missing something? Do we want to move the PHP templates to the Form component too?

---------------------------------------------------------------------------

by stof at 2012-07-31T08:28:28Z

@fabpot it is even worse than that: the FormHelper currently uses the theme by using ``$theme . ':' . $block . '.html.php`` IIRC. This is not compatible with the default template name parser of the component expecting a path. And the FrameworkBundle template name parser does not support accessing a template outside a bundle AFAIK.
So moving the templating to the component would require some refactoring in the FormHelper and the template name parser. However, I think it is worth it. Some people complained that using the form rendering (outside the full-stack framework) was requiring either setting up Twig with the bridge, or adding FrameworkBundle in the project (which means including most of the code of the full-stack framework). Having the Templating rendering in the standalone component could be a great idea

---------------------------------------------------------------------------

by fabpot at 2012-07-31T08:42:53Z

But then, I don't want to promote the Templating component or the PHP templating system. Twig is always a better alternative and this should be what people use most of the time, PHP being the rare exception.

Anyway, we are too close from the first 2.1 RC, so any big refactoring will have to wait for 2.2.

---------------------------------------------------------------------------

by stof at 2012-07-31T09:02:10Z

then maybe we should keep the FormHelper in FrameworkBundle for now as it is tied to the FrameworkBundle template name parser anyway currently.

---------------------------------------------------------------------------

by bschussek at 2012-07-31T14:22:35Z

> it it is even worse than that: the FormHelper currently uses the theme by using ``$theme . ':' . $block . '.html.php`` IIRC. This is not compatible with the default template name parser of the component expecting a path.

This is why the templates are still in FrameworkBundle. I think they should be moved too, but then we have to change

* the default theme to an absolute file path
* the FrameworkBundle name parser to accept absolute paths

I think this can wait until 2.2. Baby steps.

> I don't want to promote the Templating component or the PHP templating system.

We can both promote Twig while making Templating as easy to use as possible. If people want to use Templating, they probably have a reason. We don't have to make their lives more painful than necessary.

Btw: Templating is a *lot* faster for rendering forms than Twig. On Denis' form, Templating takes 1.15 seconds while Twig takes 2.

About moving the helpers, we have two choices:

* Move each helper to the respective component. This would not require new releases of the Templating component when we add more helpers in other component.

* Move all helpers to Templating. This does not make that much sense for Form, as then Form has support for Templating (TemplatingRendererEngine) and Templating has support for Form (FormHelper), which is a bit weird. I personally prefer a stacked architecture, where Templating is at the bottom and Form-agnostic, and Form (or any other component) builds upon that.

I'm fine with both approaches. I'll move FormHelper back to FrameworkBundle, and we can decide for a direction in 2.2.

---------------------------------------------------------------------------

by bschussek at 2012-07-31T14:36:30Z

Done.
This commit is contained in:
Fabien Potencier 2012-07-31 16:38:15 +02:00
commit ee2b8c474e
23 changed files with 1291 additions and 95 deletions

View File

@ -1271,6 +1271,25 @@
private $password;
```
* The classes `ValidatorContext` and `ValidatorFactory` were deprecated and
will be removed in Symfony 2.3. You should use the new entry point
`Validation` instead.
Before:
```
$validator = ValidatorFactory::buildDefault(array('path/to/mapping.xml'))
->getValidator();
```
After:
```
$validator = Validation::createValidatorBuilder()
->addXmlMapping('path/to/mapping.xml')
->getValidator();
```
### Session
* Flash messages now return an array based on their type. The old method is

View File

@ -11,80 +11,98 @@
namespace Symfony\Bundle\FrameworkBundle\Tests\Templating\Helper;
use Symfony\Bundle\FrameworkBundle\Templating\Helper\FormHelper;
use Symfony\Bundle\FrameworkBundle\Templating\Helper\TranslatorHelper;
use Symfony\Component\Form\FormView;
use Symfony\Component\Form\Extension\Templating\TemplatingExtension;
use Symfony\Component\Form\Tests\AbstractDivLayoutTest;
use Symfony\Bundle\FrameworkBundle\Tests\Templating\Helper\Fixtures\StubTemplateNameParser;
use Symfony\Bundle\FrameworkBundle\Tests\Templating\Helper\Fixtures\StubTranslator;
use Symfony\Component\Templating\PhpEngine;
use Symfony\Component\Templating\Loader\FilesystemLoader;
use Symfony\Component\Form\FormView;
use Symfony\Component\Form\FormRenderer;
use Symfony\Component\Form\Extension\Templating\TemplatingRendererEngine;
use Symfony\Component\Form\Tests\AbstractDivLayoutTest;
// should probably be moved to the Translation component
use Symfony\Bundle\FrameworkBundle\Templating\Helper\TranslatorHelper;
class FormHelperDivLayoutTest extends AbstractDivLayoutTest
{
protected $helper;
/**
* @var PhpEngine
*/
protected $engine;
protected function setUp()
{
parent::setUp();
if (!class_exists('Symfony\Bundle\FrameworkBundle\Templating\Helper\TranslatorHelper')) {
$this->markTestSkipped('The "FrameworkBundle" is not available');
}
$root = realpath(__DIR__.'/../../../Resources/views');
if (!class_exists('Symfony\Component\Templating\PhpEngine')) {
$this->markTestSkipped('The "Templating" component is not available');
}
parent::setUp();
}
protected function getExtensions()
{
// should be moved to the Form component once absolute file paths are supported
// by the default name parser in the Templating component
$reflClass = new \ReflectionClass('Symfony\Bundle\FrameworkBundle\FrameworkBundle');
$root = realpath(dirname($reflClass->getFileName()) . '/Resources/views');
$rootTheme = realpath(__DIR__.'/Resources');
$templateNameParser = new StubTemplateNameParser($root, $rootTheme);
$loader = new FilesystemLoader(array());
$engine = new PhpEngine($templateNameParser, $loader);
$engine->addGlobal('global', '');
$rendererEngine = new TemplatingRendererEngine($engine, array('FrameworkBundle:Form'));
$renderer = new FormRenderer($rendererEngine, $this->getMock('Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderInterface'));
$this->helper = new FormHelper($renderer);
$engine->setHelpers(array(
$this->helper,
$this->engine = new PhpEngine($templateNameParser, $loader);
$this->engine->addGlobal('global', '');
$this->engine->setHelpers(array(
new TranslatorHelper(new StubTranslator()),
));
return array_merge(parent::getExtensions(), array(
new TemplatingExtension($this->engine, $this->csrfProvider, array(
'FrameworkBundle:Form',
)),
));
}
protected function tearDown()
{
$this->helper = null;
$this->engine = null;
}
protected function renderEnctype(FormView $view)
{
return (string) $this->helper->enctype($view);
return (string) $this->engine->get('form')->enctype($view);
}
protected function renderLabel(FormView $view, $label = null, array $vars = array())
{
return (string) $this->helper->label($view, $label, $vars);
return (string) $this->engine->get('form')->label($view, $label, $vars);
}
protected function renderErrors(FormView $view)
{
return (string) $this->helper->errors($view);
return (string) $this->engine->get('form')->errors($view);
}
protected function renderWidget(FormView $view, array $vars = array())
{
return (string) $this->helper->widget($view, $vars);
return (string) $this->engine->get('form')->widget($view, $vars);
}
protected function renderRow(FormView $view, array $vars = array())
{
return (string) $this->helper->row($view, $vars);
return (string) $this->engine->get('form')->row($view, $vars);
}
protected function renderRest(FormView $view, array $vars = array())
{
return (string) $this->helper->rest($view, $vars);
return (string) $this->engine->get('form')->rest($view, $vars);
}
protected function setTheme(FormView $view, array $themes)
{
$this->helper->setTheme($view, $themes);
$this->engine->get('form')->setTheme($view, $themes);
}
public static function themeBlockInheritanceProvider()

View File

@ -11,82 +11,98 @@
namespace Symfony\Bundle\FrameworkBundle\Tests\Templating\Helper;
use Symfony\Bundle\FrameworkBundle\Templating\Helper\FormHelper;
use Symfony\Bundle\FrameworkBundle\Templating\Helper\TranslatorHelper;
use Symfony\Component\Form\FormView;
use Symfony\Component\Form\Extension\Templating\TemplatingExtension;
use Symfony\Component\Form\Tests\AbstractTableLayoutTest;
use Symfony\Bundle\FrameworkBundle\Tests\Templating\Helper\Fixtures\StubTemplateNameParser;
use Symfony\Bundle\FrameworkBundle\Tests\Templating\Helper\Fixtures\StubTranslator;
use Symfony\Component\Form\FormView;
use Symfony\Component\Form\FormRenderer;
use Symfony\Component\Form\Extension\Templating\TemplatingRendererEngine;
use Symfony\Component\Form\Tests\AbstractTableLayoutTest;
use Symfony\Component\Templating\PhpEngine;
use Symfony\Component\Templating\Loader\FilesystemLoader;
// should probably be moved to the Translation component
use Symfony\Bundle\FrameworkBundle\Templating\Helper\TranslatorHelper;
class FormHelperTableLayoutTest extends AbstractTableLayoutTest
{
protected $helper;
/**
* @var PhpEngine
*/
protected $engine;
protected function setUp()
{
parent::setUp();
if (!class_exists('Symfony\Bundle\FrameworkBundle\Templating\Helper\TranslatorHelper')) {
$this->markTestSkipped('The "FrameworkBundle" is not available');
}
$root = realpath(__DIR__.'/../../../Resources/views');
if (!class_exists('Symfony\Component\Templating\PhpEngine')) {
$this->markTestSkipped('The "Templating" component is not available');
}
parent::setUp();
}
protected function getExtensions()
{
// should be moved to the Form component once absolute file paths are supported
// by the default name parser in the Templating component
$reflClass = new \ReflectionClass('Symfony\Bundle\FrameworkBundle\FrameworkBundle');
$root = realpath(dirname($reflClass->getFileName()) . '/Resources/views');
$rootTheme = realpath(__DIR__.'/Resources');
$templateNameParser = new StubTemplateNameParser($root, $rootTheme);
$loader = new FilesystemLoader(array());
$engine = new PhpEngine($templateNameParser, $loader);
$engine->addGlobal('global', '');
$rendererEngine = new TemplatingRendererEngine($engine, array(
'FrameworkBundle:Form',
'FrameworkBundle:FormTable'
));
$renderer = new FormRenderer($rendererEngine, $this->getMock('Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderInterface'));
$this->helper = new FormHelper($renderer);
$engine->setHelpers(array(
$this->helper,
$this->engine = new PhpEngine($templateNameParser, $loader);
$this->engine->addGlobal('global', '');
$this->engine->setHelpers(array(
new TranslatorHelper(new StubTranslator()),
));
return array_merge(parent::getExtensions(), array(
new TemplatingExtension($this->engine, $this->csrfProvider, array(
'FrameworkBundle:Form',
'FrameworkBundle:FormTable',
)),
));
}
protected function tearDown()
{
$this->helper = null;
$this->engine = null;
}
protected function renderEnctype(FormView $view)
{
return (string) $this->helper->enctype($view);
return (string) $this->engine->get('form')->enctype($view);
}
protected function renderLabel(FormView $view, $label = null, array $vars = array())
{
return (string) $this->helper->label($view, $label, $vars);
return (string) $this->engine->get('form')->label($view, $label, $vars);
}
protected function renderErrors(FormView $view)
{
return (string) $this->helper->errors($view);
return (string) $this->engine->get('form')->errors($view);
}
protected function renderWidget(FormView $view, array $vars = array())
{
return (string) $this->helper->widget($view, $vars);
return (string) $this->engine->get('form')->widget($view, $vars);
}
protected function renderRow(FormView $view, array $vars = array())
{
return (string) $this->helper->row($view, $vars);
return (string) $this->engine->get('form')->row($view, $vars);
}
protected function renderRest(FormView $view, array $vars = array())
{
return (string) $this->helper->rest($view, $vars);
return (string) $this->engine->get('form')->rest($view, $vars);
}
protected function setTheme(FormView $view, array $themes)
{
$this->helper->setTheme($view, $themes);
$this->engine->get('form')->setTheme($view, $themes);
}
}

View File

@ -177,3 +177,4 @@ CHANGELOG
* made FormView properties public and deprecated their accessor methods
* made the normalized data of a form accessible in the template through the variable "form.vars.data"
* made the original data of a choice accessible in the template through the property "choice.data"
* added convenience class Forms and FormFactoryBuilderInterface

View File

@ -0,0 +1,33 @@
<?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\Component\Form\Extension\Templating;
use Symfony\Component\Form\AbstractExtension;
use Symfony\Component\Form\FormRenderer;
use Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderInterface;
use Symfony\Component\Templating\PhpEngine;
use Symfony\Bundle\FrameworkBundle\Templating\Helper\FormHelper;
/**
* Integrates the Templating component with the Form library.
*
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class TemplatingExtension extends AbstractExtension
{
public function __construct(PhpEngine $engine, CsrfProviderInterface $csrfProvider = null, array $defaultThemes = array())
{
$engine->addHelpers(array(
new FormHelper(new FormRenderer(new TemplatingRendererEngine($engine, $defaultThemes), $csrfProvider))
));
}
}

View File

@ -17,6 +17,11 @@ use Symfony\Component\Form\AbstractExtension;
use Symfony\Component\Validator\ValidatorInterface;
use Symfony\Component\Validator\Constraints\Valid;
/**
* Extension supporting the Symfony2 Validator component in forms.
*
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class ValidatorExtension extends AbstractExtension
{
private $validator;
@ -25,6 +30,12 @@ class ValidatorExtension extends AbstractExtension
{
$this->validator = $validator;
// Register the form constraints in the validator programmatically.
// This functionality is required when using the Form component without
// the DIC, where the XML file is loaded automatically. Thus the following
// code must be kept synchronized with validation.xml
/** @var \Symfony\Component\Validator\Mapping\ClassMetadata $metadata */
$metadata = $this->validator->getMetadataFactory()->getClassMetadata('Symfony\Component\Form\Form');
$metadata->addConstraint(new Form());
$metadata->addPropertyConstraint('children', new Valid());

View File

@ -37,7 +37,7 @@ class FormFactory implements FormFactoryInterface
/**
* {@inheritdoc}
*/
public function create($type, $data = null, array $options = array(), FormBuilderInterface $parent = null)
public function create($type = 'form', $data = null, array $options = array(), FormBuilderInterface $parent = null)
{
return $this->createBuilder($type, $data, $options, $parent)->getForm();
}
@ -45,7 +45,7 @@ class FormFactory implements FormFactoryInterface
/**
* {@inheritdoc}
*/
public function createNamed($name, $type, $data = null, array $options = array(), FormBuilderInterface $parent = null)
public function createNamed($name, $type = 'form', $data = null, array $options = array(), FormBuilderInterface $parent = null)
{
return $this->createNamedBuilder($name, $type, $data, $options, $parent)->getForm();
}
@ -61,7 +61,7 @@ class FormFactory implements FormFactoryInterface
/**
* {@inheritdoc}
*/
public function createBuilder($type, $data = null, array $options = array(), FormBuilderInterface $parent = null)
public function createBuilder($type = 'form', $data = null, array $options = array(), FormBuilderInterface $parent = null)
{
$name = $type instanceof FormTypeInterface || $type instanceof ResolvedFormTypeInterface
? $type->getName()
@ -73,7 +73,7 @@ class FormFactory implements FormFactoryInterface
/**
* {@inheritdoc}
*/
public function createNamedBuilder($name, $type, $data = null, array $options = array(), FormBuilderInterface $parent = null)
public function createNamedBuilder($name, $type = 'form', $data = null, array $options = array(), FormBuilderInterface $parent = null)
{
if (null !== $data && !array_key_exists('data', $options)) {
$options['data'] = $data;

View File

@ -0,0 +1,160 @@
<?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\Component\Form;
/**
* The default implementation of FormFactoryBuilderInterface.
*
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class FormFactoryBuilder implements FormFactoryBuilderInterface
{
/**
* @var ResolvedFormTypeFactoryInterface
*/
private $resolvedTypeFactory;
/**
* @var array
*/
private $extensions = array();
/**
* @var array
*/
private $types = array();
/**
* @var array
*/
private $typeExtensions = array();
/**
* @var array
*/
private $typeGuessers = array();
/**
* {@inheritdoc}
*/
public function setResolvedTypeFactory(ResolvedFormTypeFactoryInterface $resolvedTypeFactory)
{
$this->resolvedTypeFactory = $resolvedTypeFactory;
return $this;
}
/**
* {@inheritdoc}
*/
public function addExtension(FormExtensionInterface $extension)
{
$this->extensions[] = $extension;
return $this;
}
/**
* {@inheritdoc}
*/
public function addExtensions(array $extensions)
{
$this->extensions = array_merge($this->extensions, $extensions);
return $this;
}
/**
* {@inheritdoc}
*/
public function addType(FormTypeInterface $type)
{
$this->types[$type->getName()] = $type;
return $this;
}
/**
* {@inheritdoc}
*/
public function addTypes(array $types)
{
foreach ($types as $type) {
$this->types[$type->getName()] = $type;
}
return $this;
}
/**
* {@inheritdoc}
*/
public function addTypeExtension(FormTypeExtensionInterface $typeExtension)
{
$this->typeExtensions[$typeExtension->getExtendedType()][] = $typeExtension;
return $this;
}
/**
* {@inheritdoc}
*/
public function addTypeExtensions(array $typeExtensions)
{
foreach ($typeExtensions as $typeExtension) {
$this->typeExtensions[$typeExtension->getExtendedType()][] = $typeExtension;
}
return $this;
}
/**
* {@inheritdoc}
*/
public function addTypeGuesser(FormTypeGuesserInterface $typeGuesser)
{
$this->typeGuessers[] = $typeGuesser;
return $this;
}
/**
* {@inheritdoc}
*/
public function addTypeGuessers(array $typeGuessers)
{
$this->typeGuessers = array_merge($this->typeGuessers, $typeGuessers);
return $this;
}
/**
* {@inheritdoc}
*/
public function getFormFactory()
{
$extensions = $this->extensions;
if (count($this->types) > 0 || count($this->typeExtensions) > 0 || count($this->typeGuessers) > 0) {
$typeGuesser = count($this->typeGuessers) > 1
? new FormTypeGuesserChain($this->typeGuessers)
: $this->typeGuessers[0];
$extensions[] = new PreloadedExtension($this->types, $this->typeExtensions, $typeGuesser);
}
$resolvedTypeFactory = $this->resolvedTypeFactory ?: new ResolvedFormTypeFactory();
$registry = new FormRegistry($extensions, $resolvedTypeFactory);
return new FormFactory($registry, $resolvedTypeFactory);
}
}

View File

@ -0,0 +1,108 @@
<?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\Component\Form;
/**
* A builder for FormFactoryInterface objects.
*
* @author Bernhard Schussek <bschussek@gmail.com>
*/
interface FormFactoryBuilderInterface
{
/**
* Sets the factory for creating ResolvedFormTypeInterface instances.
*
* @param ResolvedFormTypeFactoryInterface $resolvedTypeFactory
*
* @return FormFactoryBuilderInterface The builder.
*/
public function setResolvedTypeFactory(ResolvedFormTypeFactoryInterface $resolvedTypeFactory);
/**
* Adds an extension to be loaded by the factory.
*
* @param FormExtensionInterface $extension The extension.
*
* @return FormFactoryBuilderInterface The builder.
*/
public function addExtension(FormExtensionInterface $extension);
/**
* Adds a list of extensions to be loaded by the factory.
*
* @param array $extensions The extensions.
*
* @return FormFactoryBuilderInterface The builder.
*/
public function addExtensions(array $extensions);
/**
* Adds a form type to the factory.
*
* @param FormTypeInterface $type The form type.
*
* @return FormFactoryBuilderInterface The builder.
*/
public function addType(FormTypeInterface $type);
/**
* Adds a list of form types to the factory.
*
* @param array $types The form types.
*
* @return FormFactoryBuilderInterface The builder.
*/
public function addTypes(array $types);
/**
* Adds a form type extension to the factory.
*
* @param FormTypeExtensionInterface $typeExtension The form type extension.
*
* @return FormFactoryBuilderInterface The builder.
*/
public function addTypeExtension(FormTypeExtensionInterface $typeExtension);
/**
* Adds a list of form type extensions to the factory.
*
* @param array $typeExtensions The form type extensions.
*
* @return FormFactoryBuilderInterface The builder.
*/
public function addTypeExtensions(array $typeExtensions);
/**
* Adds a type guesser to the factory.
*
* @param FormTypeGuesserInterface $typeGuesser The type guesser.
*
* @return FormFactoryBuilderInterface The builder.
*/
public function addTypeGuesser(FormTypeGuesserInterface $typeGuesser);
/**
* Adds a list of type guessers to the factory.
*
* @param array $typeGuessers The type guessers.
*
* @return FormFactoryBuilderInterface The builder.
*/
public function addTypeGuessers(array $typeGuessers);
/**
* Builds and returns the factory.
*
* @return FormFactoryInterface The form factory.
*/
public function getFormFactory();
}

View File

@ -30,7 +30,7 @@ interface FormFactoryInterface
*
* @throws Exception\FormException if any given option is not applicable to the given type
*/
public function create($type, $data = null, array $options = array(), FormBuilderInterface $parent = null);
public function create($type = 'form', $data = null, array $options = array(), FormBuilderInterface $parent = null);
/**
* Returns a form.
@ -47,7 +47,7 @@ interface FormFactoryInterface
*
* @throws Exception\FormException if any given option is not applicable to the given type
*/
public function createNamed($name, $type, $data = null, array $options = array(), FormBuilderInterface $parent = null);
public function createNamed($name, $type = 'form', $data = null, array $options = array(), FormBuilderInterface $parent = null);
/**
* Returns a form for a property of a class.
@ -78,7 +78,7 @@ interface FormFactoryInterface
*
* @throws Exception\FormException if any given option is not applicable to the given type
*/
public function createBuilder($type, $data = null, array $options = array(), FormBuilderInterface $parent = null);
public function createBuilder($type = 'form', $data = null, array $options = array(), FormBuilderInterface $parent = null);
/**
* Returns a form builder.
@ -93,7 +93,7 @@ interface FormFactoryInterface
*
* @throws Exception\FormException if any given option is not applicable to the given type
*/
public function createNamedBuilder($name, $type, $data = null, array $options = array(), FormBuilderInterface $parent = null);
public function createNamedBuilder($name, $type = 'form', $data = null, array $options = array(), FormBuilderInterface $parent = null);
/**
* Returns a form builder for a property of a class.

View File

@ -0,0 +1,186 @@
<?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\Component\Form;
use Symfony\Component\Form\Extension\Core\CoreExtension;
use Symfony\Component\Form\Extension\HttpFoundation\HttpFoundationExtension;
/**
* Entry point of the Form component.
*
* Use this class to conveniently create new form factories:
*
* <code>
* use Symfony\Component\Form\Forms;
*
* $formFactory = Forms::createFormFactory();
*
* $form = $formFactory->createBuilder()
* ->add('firstName', 'text')
* ->add('lastName', 'text')
* ->add('age', 'integer')
* ->add('gender', 'choice', array(
* 'choices' => array('m' => 'Male', 'f' => 'Female'),
* ))
* ->getForm();
* </code>
*
* You can also add custom extensions to the form factory:
*
* <code>
* $formFactory = Forms::createFormFactoryBuilder()
* ->addExtension(new AcmeExtension())
* ->getFormFactory();
* </code>
*
* If you create custom form types or type extensions, it is
* generally recommended to create your own extensions that lazily
* load these types and type extensions. In projects where performance
* does not matter that much, you can also pass them directly to the
* form factory:
*
* <code>
* $formFactory = Forms::createFormFactoryBuilder()
* ->addType(new PersonType())
* ->addType(new PhoneNumberType())
* ->addTypeExtension(new FormTypeHelpTextExtension())
* ->getFormFactory();
* </code>
*
* Support for CSRF protection is provided by the CsrfExtension.
* This extension needs a CSRF provider with a strong secret
* (e.g. a 20 character long random string). The default
* implementation for this is DefaultCsrfProvider:
*
* <code>
* use Symfony\Component\Form\Extension\Csrf\CsrfExtension;
* use Symfony\Component\Form\Extension\Csrf\CsrfProvider\DefaultCsrfProvider;
*
* $secret = 'V8a5Z97e...';
* $formFactory = Forms::createFormFactoryBuilder()
* ->addExtension(new CsrfExtension(new DefaultCsrfProvider($secret)))
* ->getFormFactory();
* </code>
*
* Support for the HttpFoundation is provided by the
* HttpFoundationExtension. You are also advised to load the CSRF
* extension with the driver for HttpFoundation's Session class:
*
* <code>
* use Symfony\Component\HttpFoundation\Session\Session;
* use Symfony\Component\Form\Extension\HttpFoundation\HttpFoundationExtension;
* use Symfony\Component\Form\Extension\Csrf\CsrfExtension;
* use Symfony\Component\Form\Extension\Csrf\CsrfProvider\SessionCsrfProvider;
*
* $session = new Session();
* $secret = 'V8a5Z97e...';
* $formFactory = Forms::createFormFactoryBuilder()
* ->addExtension(new HttpFoundationExtension())
* ->addExtension(new CsrfExtension(new SessionCsrfProvider($session, $secret)))
* ->getFormFactory();
* </code>
*
* Support for the Validator component is provided by ValidatorExtension.
* This extension needs a validator object to function properly:
*
* <code>
* use Symfony\Component\Validator\Validation;
* use Symfony\Component\Form\Extension\Validator\ValidatorExtension;
*
* $validator = Validation::createValidator();
* $formFactory = Forms::createFormFactoryBuilder()
* ->addExtension(new ValidatorExtension($validator))
* ->getFormFactory();
* </code>
*
* Support for the Templating component is provided by TemplatingExtension.
* This extension needs a PhpEngine object for rendering forms. As second
* argument you should pass the names of the default themes. Here is an
* example for using the default layout with "<div>" tags:
*
* <code>
* use Symfony\Component\Form\Extension\Templating\TemplatingExtension;
*
* $formFactory = Forms::createFormFactoryBuilder()
* ->addExtension(new TemplatingExtension($engine, null, array(
* 'FrameworkBundle:Form',
* )))
* ->getFormFactory();
* </code>
*
* The next example shows how to include the "<table>" layout:
*
* <code>
* use Symfony\Component\Form\Extension\Templating\TemplatingExtension;
*
* $formFactory = Forms::createFormFactoryBuilder()
* ->addExtension(new TemplatingExtension($engine, null, array(
* 'FrameworkBundle:Form',
* 'FrameworkBundle:FormTable',
* )))
* ->getFormFactory();
* </code>
*
* If you also loaded the CsrfExtension, you should pass the CSRF provider
* to the extension so that you can render CSRF tokens in your templates
* more easily:
*
* <code>
* use Symfony\Component\Form\Extension\Csrf\CsrfExtension;
* use Symfony\Component\Form\Extension\Csrf\CsrfProvider\DefaultCsrfProvider;
* use Symfony\Component\Form\Extension\Templating\TemplatingExtension;
*
*
* $secret = 'V8a5Z97e...';
* $csrfProvider = new DefaultCsrfProvider($secret);
* $formFactory = Forms::createFormFactoryBuilder()
* ->addExtension(new CsrfExtension($csrfProvider))
* ->addExtension(new TemplatingExtension($engine, $csrfProvider, array(
* 'FrameworkBundle:Form',
* )))
* ->getFormFactory();
* </code>
*
* @author Bernhard Schussek <bschussek@gmail.com>
*/
final class Forms
{
/**
* Creates a form factory with the default configuration.
*
* @return FormFactoryInterface The form factory.
*/
public static function createFormFactory()
{
return self::createFormFactoryBuilder()->getFormFactory();
}
/**
* Creates a form factory builder with the default configuration.
*
* @return FormFactoryBuilderInterface The form factory builder.
*/
public static function createFormFactoryBuilder()
{
$builder = new FormFactoryBuilder();
$builder->addExtension(new CoreExtension());
return $builder;
}
/**
* This class cannot be instantiated.
*/
private function __construct()
{
}
}

View File

@ -0,0 +1,97 @@
<?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\Component\Form;
use Symfony\Component\Form\Exception\FormException;
/**
* A form extension with preloaded types, type exceptions and type guessers.
*
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class PreloadedExtension implements FormExtensionInterface
{
/**
* @var array
*/
private $types = array();
/**
* @var array
*/
private $typeExtensions = array();
/**
* @var FormTypeGuesserInterface
*/
private $typeGuesser;
/**
* Creates a new preloaded extension.
*
* @param array $types The types that the extension should support.
* @param array $typeExtensions The type extensions that the extension should support.
* @param FormTypeGuesserInterface $typeGuesser The guesser that the extension should support.
*/
public function __construct(array $types, array $typeExtensions, FormTypeGuesserInterface $typeGuesser)
{
$this->types = $types;
$this->typeExtensions = $typeExtensions;
$this->typeGuesser = $typeGuesser;
}
/**
* {@inheritdoc}
*/
public function getType($name)
{
if (!isset($this->types[$name])) {
throw new FormException(sprintf('The type "%s" can not be loaded by this extension', $name));
}
return $this->types[$name];
}
/**
* {@inheritdoc}
*/
public function hasType($name)
{
return isset($this->types[$name]);
}
/**
* {@inheritdoc}
*/
public function getTypeExtensions($name)
{
return isset($this->typeExtensions[$name])
? $this->typeExtensions[$name]
: array();
}
/**
* {@inheritdoc}
*/
public function hasTypeExtensions($name)
{
return !empty($this->typeExtensions[$name]);
}
/**
* {@inheritdoc}
*/
public function getTypeGuesser()
{
return $this->typeGuesser;
}
}

View File

@ -39,7 +39,6 @@ abstract class AbstractLayoutTest extends FormIntegrationTestCase
protected function getExtensions()
{
return array(
new CoreExtension(),
new CsrfExtension($this->csrfProvider),
);
}

View File

@ -11,9 +11,7 @@
namespace Symfony\Component\Form\Tests;
use Symfony\Component\Form\FormFactory;
use Symfony\Component\Form\ResolvedFormTypeFactory;
use Symfony\Component\Form\FormRegistry;
use Symfony\Component\Form\Forms;
use Symfony\Component\Form\Extension\Core\CoreExtension;
/**
@ -22,17 +20,7 @@ use Symfony\Component\Form\Extension\Core\CoreExtension;
class FormIntegrationTestCase extends \PHPUnit_Framework_TestCase
{
/**
* @var ResolvedFormTypeFactory
*/
protected $resolvedTypeFactory;
/**
* @var FormRegistry
*/
protected $registry;
/**
* @var FormFactory
* @var \Symfony\Component\Form\FormFactoryInterface
*/
protected $factory;
@ -42,15 +30,13 @@ class FormIntegrationTestCase extends \PHPUnit_Framework_TestCase
$this->markTestSkipped('The "EventDispatcher" component is not available');
}
$this->resolvedTypeFactory = new ResolvedFormTypeFactory();
$this->registry = new FormRegistry($this->getExtensions(), $this->resolvedTypeFactory);
$this->factory = new FormFactory($this->registry, $this->resolvedTypeFactory);
$this->factory = Forms::createFormFactoryBuilder()
->addExtensions($this->getExtensions())
->getFormFactory();
}
protected function getExtensions()
{
return array(
new CoreExtension(),
);
return array();
}
}

View File

@ -26,3 +26,5 @@ CHANGELOG
* added Range constraint
* deprecated the Min and Max constraints
* deprecated the MinLength and MaxLength constraints
* added Validation and ValidatorBuilderInterface
* deprecated ValidatorContext, ValidatorContextInterface and ValidatorFactory

View File

@ -35,7 +35,7 @@ class ClassMetadataFactory implements ClassMetadataFactoryInterface
protected $loadedClasses = array();
public function __construct(LoaderInterface $loader, CacheInterface $cache = null)
public function __construct(LoaderInterface $loader = null, CacheInterface $cache = null)
{
$this->loader = $loader;
$this->cache = $cache;
@ -68,9 +68,11 @@ class ClassMetadataFactory implements ClassMetadataFactoryInterface
$metadata->mergeConstraints($this->getClassMetadata($interface->name));
}
$this->loader->loadClassMetadata($metadata);
if (null !== $this->loader) {
$this->loader->loadClassMetadata($metadata);
}
if ($this->cache !== null) {
if (null !== $this->cache) {
$this->cache->write($metadata);
}

View File

@ -2,19 +2,34 @@ Validator Component
===================
This component is based on the JSR-303 Bean Validation specification and
enables specifying validation rules for classes using XML, YAML or
enables specifying validation rules for classes using XML, YAML, PHP or
annotations, which can then be checked against instances of these classes.
use Symfony\Component\Validator\Validator;
use Symfony\Component\Validator\Mapping\ClassMetadataFactory;
use Symfony\Component\Validator\Mapping\Loader\StaticMethodLoader;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\ConstraintValidatorFactory;
Usage
-----
$validator = new Validator(
new ClassMetadataFactory(new StaticMethodLoader()),
new ConstraintValidatorFactory()
);
The component provides "validation constraints", which are simple objects
containing the rules for the validation. Let's validate a simple string
as an example:
use Symfony\Component\Validator\Validation;
use Symfony\Component\Validator\Constraints\MinLength;
$validator = Validation::createValidator();
$violations = $validator->validateValue('Bernhard', new MinLength(10));
This validation will fail because the given string is shorter than ten
characters. The precise errors, here called "constraint violations", are
returned by the validator. You can analyze these or return them to the user.
If the violation list is empty, validation succeeded.
Validation of arrays is possible using the `Collection` constraint:
use Symfony\Component\Validator\Validation;
use Symfony\Component\Validator\Constraints as Assert;
$validator = Validation::createValidator();
$constraint = new Assert\Collection(array(
'name' => new Assert\Collection(array(
@ -30,6 +45,59 @@ annotations, which can then be checked against instances of these classes.
$violations = $validator->validateValue($input, $constraint);
Again, the validator returns the list of violations.
Validation of objects is possible using "constraint mapping". With such
a mapping you can put constraints onto properties and objects of classes.
Whenever an object of this class is validated, its properties and
method results are matched against the constraints.
use Symfony\Component\Validator\Validation;
use Symfony\Component\Validator\Constraints as Assert;
class User
{
/**
* @Assert\MinLength(3)
* @Assert\NotBlank
*/
private $name;
/**
* @Assert\Email
* @Assert\NotBlank
*/
private $email;
public function __construct($name, $email)
{
$this->name = $name;
$this->email = $email;
}
/**
* @Assert\True(message = "The user should have a Google Mail account")
*/
public function isGmailUser()
{
return false !== strpos($this->email, '@gmail.com');
}
}
$validator = Validation::createValidatorBuilder()
->enableAnnotationMapping()
->getValidator();
$user = new User('John Doe', 'john@example.com');
$violations = $validator->validate($user);
This example uses the annotation support of Doctrine Common to
map constraints to properties and methods. You can also map constraints
using XML, YAML or plain PHP, if you dislike annotations or don't want
to include Doctrine. Check the documentation for more information about
these drivers.
Resources
---------

View File

@ -0,0 +1,50 @@
<?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\Component\Validator;
/**
* Entry point for the Validator component.
*
* @author Bernhard Schussek <bschussek@gmail.com>
*/
final class Validation
{
/**
* Creates a new validator.
*
* If you want to configure the validator, use
* {@link createValidatorBuilder()} instead.
*
* @return ValidatorInterface The new validator.
*/
public static function createValidator()
{
return self::createValidatorBuilder()->getValidator();
}
/**
* Creates a configurable builder for validator objects.
*
* @return ValidatorBuilderInterface The new builder.
*/
public static function createValidatorBuilder()
{
return new ValidatorBuilder();
}
/**
* This class cannot be instantiated.
*/
private function __construct()
{
}
}

View File

@ -0,0 +1,271 @@
<?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\Component\Validator;
use Symfony\Component\Validator\Mapping\ClassMetadataFactory;
use Symfony\Component\Validator\Exception\ValidatorException;
use Symfony\Component\Validator\Mapping\BlackholeMetadataFactory;
use Symfony\Component\Validator\Mapping\Loader\LoaderChain;
use Symfony\Component\Validator\Mapping\ClassMetadataFactoryInterface;
use Symfony\Component\Validator\Mapping\Cache\CacheInterface;
use Symfony\Component\Validator\Mapping\Loader\StaticMethodLoader;
use Symfony\Component\Validator\Mapping\Loader\YamlFileLoader;
use Symfony\Component\Validator\Mapping\Loader\AnnotationLoader;
use Symfony\Component\Validator\Mapping\Loader\YamlFilesLoader;
use Symfony\Component\Validator\Mapping\Loader\XmlFileLoader;
use Symfony\Component\Validator\Mapping\Loader\XmlFilesLoader;
use Doctrine\Common\Annotations\Reader;
use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Annotations\CachedReader;
use Doctrine\Common\Cache\ArrayCache;
/**
* The default implementation of {@link ValidatorBuilderInterface}.
*
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class ValidatorBuilder implements ValidatorBuilderInterface
{
/**
* @var array
*/
private $initializers = array();
/**
* @var array
*/
private $xmlMappings = array();
/**
* @var array
*/
private $yamlMappings = array();
/**
* @var array
*/
private $methodMappings = array();
/**
* @var Reader
*/
private $annotationReader = null;
/**
* @var ClassMetadataFactoryInterface
*/
private $metadataFactory;
/**
* @var ConstraintValidatorFactoryInterface
*/
private $validatorFactory;
/**
* @var CacheInterface
*/
private $metadataCache;
/**
* {@inheritdoc}
*/
public function addObjectInitializer(ObjectInitializerInterface $initializer)
{
$this->initializers[] = $initializer;
}
/**
* {@inheritdoc}
*/
public function addObjectInitializers(array $initializers)
{
$this->initializers = array_merge($this->initializers, $initializers);
}
/**
* {@inheritdoc}
*/
public function addXmlMapping($path)
{
if (null !== $this->metadataFactory) {
throw new ValidatorException('You cannot add custom mappings after setting a custom metadata factory. Configure your metadata factory instead.');
}
$this->xmlMappings[] = $path;
}
/**
* {@inheritdoc}
*/
public function addXmlMappings(array $paths)
{
if (null !== $this->metadataFactory) {
throw new ValidatorException('You cannot add custom mappings after setting a custom metadata factory. Configure your metadata factory instead.');
}
$this->xmlMappings = array_merge($this->xmlMappings, $paths);
}
/**
* {@inheritdoc}
*/
public function addYamlMapping($path)
{
if (null !== $this->metadataFactory) {
throw new ValidatorException('You cannot add custom mappings after setting a custom metadata factory. Configure your metadata factory instead.');
}
$this->yamlMappings[] = $path;
}
/**
* {@inheritdoc}
*/
public function addYamlMappings(array $paths)
{
if (null !== $this->metadataFactory) {
throw new ValidatorException('You cannot add custom mappings after setting a custom metadata factory. Configure your metadata factory instead.');
}
$this->yamlMappings = array_merge($this->yamlMappings, $paths);
}
/**
* {@inheritdoc}
*/
public function addMethodMapping($methodName)
{
if (null !== $this->metadataFactory) {
throw new ValidatorException('You cannot add custom mappings after setting a custom metadata factory. Configure your metadata factory instead.');
}
$this->methodMappings[] = $methodName;
}
/**
* {@inheritdoc}
*/
public function addMethodMappings(array $methodNames)
{
if (null !== $this->metadataFactory) {
throw new ValidatorException('You cannot add custom mappings after setting a custom metadata factory. Configure your metadata factory instead.');
}
$this->methodMappings = array_merge($this->methodMappings, $methodNames);
}
/**
* {@inheritdoc}
*/
public function enableAnnotationMapping(Reader $annotationReader = null)
{
if (null !== $this->metadataFactory) {
throw new ValidatorException('You cannot enable annotation mapping after setting a custom metadata factory. Configure your metadata factory instead.');
}
if (null === $annotationReader) {
if (!class_exists('Doctrine\Common\Annotations\AnnotationReader')) {
throw new \RuntimeException('Requested a ValidatorFactory with an AnnotationLoader, but the AnnotationReader was not found. You should add Doctrine Common to your project.');
}
$annotationReader = new CachedReader(new AnnotationReader(), new ArrayCache());
}
$this->annotationReader = $annotationReader;
}
/**
* {@inheritdoc}
*/
public function disableAnnotationMapping()
{
$this->annotationReader = null;
}
/**
* {@inheritdoc}
*/
public function setMetadataFactory(ClassMetadataFactoryInterface $metadataFactory)
{
if (count($this->xmlMappings) > 0 || count($this->yamlMappings) > 0 || count($this->methodMappings) > 0 || null !== $this->annotationReader) {
throw new ValidatorException('You cannot set a custom metadata factory after adding custom mappings. You should do either of both.');
}
$this->metadataFactory = $metadataFactory;
}
/**
* {@inheritdoc}
*/
public function setMetadataCache(CacheInterface $cache)
{
if (null !== $this->metadataFactory) {
throw new ValidatorException('You cannot set a custom metadata cache after setting a custom metadata factory. Configure your metadata factory instead.');
}
$this->metadataCache = $cache;
}
/**
* {@inheritdoc}
*/
public function setConstraintValidatorFactory(ConstraintValidatorFactoryInterface $validatorFactory)
{
$this->validatorFactory = $validatorFactory;
}
/**
* {@inheritdoc}
*/
public function getValidator()
{
$metadataFactory = $this->metadataFactory;
if (!$metadataFactory) {
$loaders = array();
if (count($this->xmlMappings) > 1) {
$loaders[] = new XmlFilesLoader($this->xmlMappings);
} elseif (1 === count($this->xmlMappings)) {
$loaders[] = new XmlFileLoader($this->xmlMappings[0]);
}
if (count($this->yamlMappings) > 1) {
$loaders[] = new YamlFilesLoader($this->yamlMappings);
} elseif (1 === count($this->yamlMappings)) {
$loaders[] = new YamlFileLoader($this->yamlMappings[0]);
}
foreach ($this->methodMappings as $methodName) {
$loaders[] = new StaticMethodLoader($methodName);
}
if ($this->annotationReader) {
$loaders[] = new AnnotationLoader($this->annotationReader);
}
$loader = null;
if (count($loaders) > 1) {
$loader = new LoaderChain($loaders);
} elseif (1 === count($loaders)) {
$loader = $loaders[0];
}
$metadataFactory = new ClassMetadataFactory($loader, $this->metadataCache);
}
$validatorFactory = $this->validatorFactory ?: new ConstraintValidatorFactory();
return new Validator($metadataFactory, $validatorFactory, $this->initializers);
}
}

View File

@ -0,0 +1,120 @@
<?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\Component\Validator;
use Symfony\Component\Validator\Mapping\ClassMetadataFactoryInterface;
use Symfony\Component\Validator\Mapping\Cache\CacheInterface;
use Doctrine\Common\Annotations\Reader;
/**
* A configurable builder for ValidatorInterface objects.
*
* @author Bernhard Schussek <bschussek@gmail.com>
*/
interface ValidatorBuilderInterface
{
/**
* Adds an object initializer to the validator.
*
* @param ObjectInitializerInterface $initializer The initializer.
*/
public function addObjectInitializer(ObjectInitializerInterface $initializer);
/**
* Adds a list of object initializers to the validator.
*
* @param array $initializers The initializer.
*/
public function addObjectInitializers(array $initializers);
/**
* Adds an XML constraint mapping file to the validator.
*
* @param string $path The path to the mapping file.
*/
public function addXmlMapping($path);
/**
* Adds a list of XML constraint mapping files to the validator.
*
* @param array $paths The paths to the mapping files.
*/
public function addXmlMappings(array $paths);
/**
* Adds a YAML constraint mapping file to the validator.
*
* @param string $path The path to the mapping file.
*/
public function addYamlMapping($path);
/**
* Adds a list of YAML constraint mappings file to the validator.
*
* @param array $paths The paths to the mapping files.
*/
public function addYamlMappings(array $paths);
/**
* Enables constraint mapping using the given static method.
*
* @param string $methodName The name of the method.
*/
public function addMethodMapping($methodName);
/**
* Enables constraint mapping using the given static methods.
*
* @param array $methodNames The names of the methods.
*/
public function addMethodMappings(array $methodNames);
/**
* Enables annotation based constraint mapping.
*
* @param Reader $annotationReader The annotation reader to be used.
*/
public function enableAnnotationMapping(Reader $annotationReader = null);
/**
* Disables annotation based constraint mapping.
*/
public function disableAnnotationMapping();
/**
* Sets the class metadata factory used by the validator.
*
* @param ClassMetadataFactoryInterface $metadataFactory The metadata factory.
*/
public function setMetadataFactory(ClassMetadataFactoryInterface $metadataFactory);
/**
* Sets the cache for caching class metadata.
*
* @param CacheInterface $cache The cache instance.
*/
public function setMetadataCache(CacheInterface $cache);
/**
* Sets the constraint validator factory used by the validator.
*
* @param ConstraintValidatorFactoryInterface $validatorFactory The validator factory.
*/
public function setConstraintValidatorFactory(ConstraintValidatorFactoryInterface $validatorFactory);
/**
* Builds and returns a new validator object.
*
* @return ValidatorInterface The built validator.
*/
public function getValidator();
}

View File

@ -17,6 +17,9 @@ use Symfony\Component\Validator\Mapping\ClassMetadataFactoryInterface;
* Default implementation of ValidatorContextInterface
*
* @author Bernhard Schussek <bschussek@gmail.com>
*
* @deprecated Deprecated since version 2.1, to be removed in 2.3. Use
* {@link Validation::createValidatorBuilder()} instead.
*/
class ValidatorContext implements ValidatorContextInterface
{
@ -34,6 +37,9 @@ class ValidatorContext implements ValidatorContextInterface
/**
* {@inheritDoc}
*
* @deprecated Deprecated since version 2.1, to be removed in 2.3. Use
* {@link Validation::createValidatorBuilder()} instead.
*/
public function setClassMetadataFactory(ClassMetadataFactoryInterface $classMetadataFactory)
{
@ -44,6 +50,9 @@ class ValidatorContext implements ValidatorContextInterface
/**
* {@inheritDoc}
*
* @deprecated Deprecated since version 2.1, to be removed in 2.3. Use
* {@link Validation::createValidatorBuilder()} instead.
*/
public function setConstraintValidatorFactory(ConstraintValidatorFactoryInterface $constraintValidatorFactory)
{
@ -54,6 +63,9 @@ class ValidatorContext implements ValidatorContextInterface
/**
* {@inheritDoc}
*
* @deprecated Deprecated since version 2.1, to be removed in 2.3. Use
* {@link Validation::createValidator()} instead.
*/
public function getValidator()
{
@ -67,6 +79,8 @@ class ValidatorContext implements ValidatorContextInterface
* Returns the class metadata factory used in the new validator
*
* @return ClassMetadataFactoryInterface The factory instance
*
* @deprecated Deprecated since version 2.1, to be removed in 2.3.
*/
public function getClassMetadataFactory()
{
@ -77,6 +91,8 @@ class ValidatorContext implements ValidatorContextInterface
* Returns the constraint validator factory used in the new validator
*
* @return ConstraintValidatorFactoryInterface The factory instance
*
* @deprecated Deprecated since version 2.1, to be removed in 2.3.
*/
public function getConstraintValidatorFactory()
{

View File

@ -27,6 +27,9 @@ use Symfony\Component\Validator\Mapping\ClassMetadataFactoryInterface;
* </code>
*
* @author Bernhard Schussek <bschussek@gmail.com>
*
* @deprecated Deprecated since version 2.1, to be removed in 2.3. Use
* {@link Validation::createValidatorBuilder()} instead.
*/
interface ValidatorContextInterface
{
@ -34,6 +37,9 @@ interface ValidatorContextInterface
* Sets the class metadata factory used in the new validator
*
* @param ClassMetadataFactoryInterface $classMetadataFactory The factory instance
*
* @deprecated Deprecated since version 2.1, to be removed in 2.3. Use
* {@link Validation::createValidatorBuilder()} instead.
*/
public function setClassMetadataFactory(ClassMetadataFactoryInterface $classMetadataFactory);
@ -41,6 +47,9 @@ interface ValidatorContextInterface
* Sets the constraint validator factory used in the new validator
*
* @param ConstraintValidatorFactoryInterface $constraintValidatorFactory The factory instance
*
* @deprecated Deprecated since version 2.1, to be removed in 2.3. Use
* {@link Validation::createValidatorBuilder()} instead.
*/
public function setConstraintValidatorFactory(ConstraintValidatorFactoryInterface $constraintValidatorFactory);
@ -48,6 +57,9 @@ interface ValidatorContextInterface
* Creates a new validator with the settings stored in this context
*
* @return ValidatorInterface The new validator
*
* @deprecated Deprecated since version 2.1, to be removed in 2.3. Use
* {@link Validation::createValidator()} instead.
*/
public function getValidator();
}

View File

@ -73,6 +73,9 @@ use Symfony\Component\Validator\Mapping\Loader\LoaderChain;
* ValidatorFactory instances should be cached and reused in your application.
*
* @author Bernhard Schussek <bschussek@gmail.com>
*
* @deprecated Deprecated since version 2.1, to be removed in 2.3. Use
* {@link Validation::createValidatorBuilder()} instead.
*/
class ValidatorFactory implements ValidatorContextInterface
{
@ -94,9 +97,15 @@ class ValidatorFactory implements ValidatorContextInterface
* use, if static method loading should
* be enabled
*
* @return ValidatorFactory The validator factory.
*
* @throws MappingException If any of the files in $mappingFiles
* has neither the extension ".xml" nor
* ".yml" nor ".yaml"
* @throws \RuntimeException If annotations are not supported.
*
* @deprecated Deprecated since version 2.1, to be removed in 2.3. Use
* {@link Validation::createValidatorBuilder()} instead.
*/
public static function buildDefault(array $mappingFiles = array(), $annotations = false, $staticMethod = null)
{
@ -155,6 +164,9 @@ class ValidatorFactory implements ValidatorContextInterface
* Sets the given context as default context
*
* @param ValidatorContextInterface $defaultContext A preconfigured context
*
* @deprecated Deprecated since version 2.1, to be removed in 2.3. Use
* {@link Validation::createValidatorBuilder()} instead.
*/
public function __construct(ValidatorContextInterface $defaultContext = null)
{
@ -168,6 +180,9 @@ class ValidatorFactory implements ValidatorContextInterface
* @param ClassMetadataFactoryInterface $metadataFactory The new factory instance
*
* @return ValidatorContextInterface The preconfigured form context
*
* @deprecated Deprecated since version 2.1, to be removed in 2.3. Use
* {@link Validation::createValidatorBuilder()} instead.
*/
public function setClassMetadataFactory(ClassMetadataFactoryInterface $metadataFactory)
{
@ -183,6 +198,9 @@ class ValidatorFactory implements ValidatorContextInterface
* @param ClassMetadataFactoryInterface $validatorFactory The new factory instance
*
* @return ValidatorContextInterface The preconfigured form context
*
* @deprecated Deprecated since version 2.1, to be removed in 2.3. Use
* {@link Validation::createValidatorBuilder()} instead.
*/
public function setConstraintValidatorFactory(ConstraintValidatorFactoryInterface $validatorFactory)
{
@ -195,6 +213,9 @@ class ValidatorFactory implements ValidatorContextInterface
* Creates a new validator with the settings stored in the default context
*
* @return ValidatorInterface The new validator
*
* @deprecated Deprecated since version 2.1, to be removed in 2.3. Use
* {@link Validation::createValidator()} instead.
*/
public function getValidator()
{