diff --git a/UPGRADE-2.1.md b/UPGRADE-2.1.md index 4112e66956..315576de6d 100644 --- a/UPGRADE-2.1.md +++ b/UPGRADE-2.1.md @@ -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 diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/FormHelperDivLayoutTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/FormHelperDivLayoutTest.php index cc386971f9..9c077f39da 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/FormHelperDivLayoutTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/FormHelperDivLayoutTest.php @@ -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() diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/FormHelperTableLayoutTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/FormHelperTableLayoutTest.php index ad8b74eba9..1f5292bc7e 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/FormHelperTableLayoutTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/FormHelperTableLayoutTest.php @@ -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); } } diff --git a/src/Symfony/Component/Form/CHANGELOG.md b/src/Symfony/Component/Form/CHANGELOG.md index 1e3a60fad3..d8c4589db2 100644 --- a/src/Symfony/Component/Form/CHANGELOG.md +++ b/src/Symfony/Component/Form/CHANGELOG.md @@ -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 diff --git a/src/Symfony/Component/Form/Extension/Templating/TemplatingExtension.php b/src/Symfony/Component/Form/Extension/Templating/TemplatingExtension.php new file mode 100644 index 0000000000..573cb518d4 --- /dev/null +++ b/src/Symfony/Component/Form/Extension/Templating/TemplatingExtension.php @@ -0,0 +1,33 @@ + + * + * 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 + */ +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)) + )); + } +} diff --git a/src/Symfony/Component/Form/Extension/Validator/ValidatorExtension.php b/src/Symfony/Component/Form/Extension/Validator/ValidatorExtension.php index 4abd9b45f8..3a70b6215e 100644 --- a/src/Symfony/Component/Form/Extension/Validator/ValidatorExtension.php +++ b/src/Symfony/Component/Form/Extension/Validator/ValidatorExtension.php @@ -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 + */ 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()); diff --git a/src/Symfony/Component/Form/FormFactory.php b/src/Symfony/Component/Form/FormFactory.php index 4ecbfb49a3..02be3017a2 100644 --- a/src/Symfony/Component/Form/FormFactory.php +++ b/src/Symfony/Component/Form/FormFactory.php @@ -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; diff --git a/src/Symfony/Component/Form/FormFactoryBuilder.php b/src/Symfony/Component/Form/FormFactoryBuilder.php new file mode 100644 index 0000000000..65f697902c --- /dev/null +++ b/src/Symfony/Component/Form/FormFactoryBuilder.php @@ -0,0 +1,160 @@ + + * + * 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 + */ +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); + } +} diff --git a/src/Symfony/Component/Form/FormFactoryBuilderInterface.php b/src/Symfony/Component/Form/FormFactoryBuilderInterface.php new file mode 100644 index 0000000000..9370c573a6 --- /dev/null +++ b/src/Symfony/Component/Form/FormFactoryBuilderInterface.php @@ -0,0 +1,108 @@ + + * + * 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 + */ +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(); +} diff --git a/src/Symfony/Component/Form/FormFactoryInterface.php b/src/Symfony/Component/Form/FormFactoryInterface.php index 73f31aa373..fc90068479 100644 --- a/src/Symfony/Component/Form/FormFactoryInterface.php +++ b/src/Symfony/Component/Form/FormFactoryInterface.php @@ -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. diff --git a/src/Symfony/Component/Form/Forms.php b/src/Symfony/Component/Form/Forms.php new file mode 100644 index 0000000000..69e104f8d8 --- /dev/null +++ b/src/Symfony/Component/Form/Forms.php @@ -0,0 +1,186 @@ + + * + * 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: + * + * + * 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(); + * + * + * You can also add custom extensions to the form factory: + * + * + * $formFactory = Forms::createFormFactoryBuilder() + * ->addExtension(new AcmeExtension()) + * ->getFormFactory(); + * + * + * 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: + * + * + * $formFactory = Forms::createFormFactoryBuilder() + * ->addType(new PersonType()) + * ->addType(new PhoneNumberType()) + * ->addTypeExtension(new FormTypeHelpTextExtension()) + * ->getFormFactory(); + * + * + * 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: + * + * + * 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(); + * + * + * 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: + * + * + * 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(); + * + * + * Support for the Validator component is provided by ValidatorExtension. + * This extension needs a validator object to function properly: + * + * + * use Symfony\Component\Validator\Validation; + * use Symfony\Component\Form\Extension\Validator\ValidatorExtension; + * + * $validator = Validation::createValidator(); + * $formFactory = Forms::createFormFactoryBuilder() + * ->addExtension(new ValidatorExtension($validator)) + * ->getFormFactory(); + * + * + * 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 "
" tags: + * + * + * use Symfony\Component\Form\Extension\Templating\TemplatingExtension; + * + * $formFactory = Forms::createFormFactoryBuilder() + * ->addExtension(new TemplatingExtension($engine, null, array( + * 'FrameworkBundle:Form', + * ))) + * ->getFormFactory(); + * + * + * The next example shows how to include the "" layout: + * + * + * use Symfony\Component\Form\Extension\Templating\TemplatingExtension; + * + * $formFactory = Forms::createFormFactoryBuilder() + * ->addExtension(new TemplatingExtension($engine, null, array( + * 'FrameworkBundle:Form', + * 'FrameworkBundle:FormTable', + * ))) + * ->getFormFactory(); + * + * + * 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: + * + * + * 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(); + * + * + * @author Bernhard Schussek + */ +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() + { + } +} diff --git a/src/Symfony/Component/Form/PreloadedExtension.php b/src/Symfony/Component/Form/PreloadedExtension.php new file mode 100644 index 0000000000..8a4eb5f319 --- /dev/null +++ b/src/Symfony/Component/Form/PreloadedExtension.php @@ -0,0 +1,97 @@ + + * + * 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 + */ +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; + } +} diff --git a/src/Symfony/Component/Form/Tests/AbstractLayoutTest.php b/src/Symfony/Component/Form/Tests/AbstractLayoutTest.php index e938a5fda8..d4ef19b259 100644 --- a/src/Symfony/Component/Form/Tests/AbstractLayoutTest.php +++ b/src/Symfony/Component/Form/Tests/AbstractLayoutTest.php @@ -39,7 +39,6 @@ abstract class AbstractLayoutTest extends FormIntegrationTestCase protected function getExtensions() { return array( - new CoreExtension(), new CsrfExtension($this->csrfProvider), ); } diff --git a/src/Symfony/Component/Form/Tests/FormIntegrationTestCase.php b/src/Symfony/Component/Form/Tests/FormIntegrationTestCase.php index bdb93e19e7..2d2175d71d 100644 --- a/src/Symfony/Component/Form/Tests/FormIntegrationTestCase.php +++ b/src/Symfony/Component/Form/Tests/FormIntegrationTestCase.php @@ -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(); } } diff --git a/src/Symfony/Component/Validator/CHANGELOG.md b/src/Symfony/Component/Validator/CHANGELOG.md index e49881a528..47687ba91a 100644 --- a/src/Symfony/Component/Validator/CHANGELOG.md +++ b/src/Symfony/Component/Validator/CHANGELOG.md @@ -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 diff --git a/src/Symfony/Component/Validator/Mapping/ClassMetadataFactory.php b/src/Symfony/Component/Validator/Mapping/ClassMetadataFactory.php index 7896152879..f84d7a3645 100644 --- a/src/Symfony/Component/Validator/Mapping/ClassMetadataFactory.php +++ b/src/Symfony/Component/Validator/Mapping/ClassMetadataFactory.php @@ -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); } diff --git a/src/Symfony/Component/Validator/README.md b/src/Symfony/Component/Validator/README.md index 291a672068..2901b7cdd9 100644 --- a/src/Symfony/Component/Validator/README.md +++ b/src/Symfony/Component/Validator/README.md @@ -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 --------- diff --git a/src/Symfony/Component/Validator/Validation.php b/src/Symfony/Component/Validator/Validation.php new file mode 100644 index 0000000000..de77e838fb --- /dev/null +++ b/src/Symfony/Component/Validator/Validation.php @@ -0,0 +1,50 @@ + + * + * 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 + */ +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() + { + } +} diff --git a/src/Symfony/Component/Validator/ValidatorBuilder.php b/src/Symfony/Component/Validator/ValidatorBuilder.php new file mode 100644 index 0000000000..7819cac5c2 --- /dev/null +++ b/src/Symfony/Component/Validator/ValidatorBuilder.php @@ -0,0 +1,271 @@ + + * + * 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 + */ +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); + } +} diff --git a/src/Symfony/Component/Validator/ValidatorBuilderInterface.php b/src/Symfony/Component/Validator/ValidatorBuilderInterface.php new file mode 100644 index 0000000000..42e1fe1272 --- /dev/null +++ b/src/Symfony/Component/Validator/ValidatorBuilderInterface.php @@ -0,0 +1,120 @@ + + * + * 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 + */ +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(); +} diff --git a/src/Symfony/Component/Validator/ValidatorContext.php b/src/Symfony/Component/Validator/ValidatorContext.php index efe23e28db..b18bb97a89 100644 --- a/src/Symfony/Component/Validator/ValidatorContext.php +++ b/src/Symfony/Component/Validator/ValidatorContext.php @@ -17,6 +17,9 @@ use Symfony\Component\Validator\Mapping\ClassMetadataFactoryInterface; * Default implementation of ValidatorContextInterface * * @author Bernhard Schussek + * + * @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() { diff --git a/src/Symfony/Component/Validator/ValidatorContextInterface.php b/src/Symfony/Component/Validator/ValidatorContextInterface.php index ffe8b8f510..73ce6b6c99 100644 --- a/src/Symfony/Component/Validator/ValidatorContextInterface.php +++ b/src/Symfony/Component/Validator/ValidatorContextInterface.php @@ -27,6 +27,9 @@ use Symfony\Component\Validator\Mapping\ClassMetadataFactoryInterface; * * * @author Bernhard Schussek + * + * @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(); } diff --git a/src/Symfony/Component/Validator/ValidatorFactory.php b/src/Symfony/Component/Validator/ValidatorFactory.php index d48d484626..5e16b2e318 100644 --- a/src/Symfony/Component/Validator/ValidatorFactory.php +++ b/src/Symfony/Component/Validator/ValidatorFactory.php @@ -73,6 +73,9 @@ use Symfony\Component\Validator\Mapping\Loader\LoaderChain; * ValidatorFactory instances should be cached and reused in your application. * * @author Bernhard Schussek + * + * @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() {