Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 31, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions UPGRADE-2.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
if (!class_exists('Symfony\Bundle\FrameworkBundle\Templating\Helper\TranslatorHelper')) {
$this->markTestSkipped('The "FrameworkBundle" is not available');
}

if (!class_exists('Symfony\Component\Templating\PhpEngine')) {
$this->markTestSkipped('The "Templating" component is not available');
}

parent::setUp();
}

$root = realpath(__DIR__.'/../../../Resources/views');
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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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\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\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\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()
{
if (!class_exists('Symfony\Bundle\FrameworkBundle\Templating\Helper\TranslatorHelper')) {
$this->markTestSkipped('The "FrameworkBundle" is not available');
}

if (!class_exists('Symfony\Component\Templating\PhpEngine')) {
$this->markTestSkipped('The "Templating" component is not available');
}

parent::setUp();
}

$root = realpath(__DIR__.'/../../../Resources/views');
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);
}
}
1 change: 1 addition & 0 deletions src/Symfony/Component/Form/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* 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 <[email protected]>
*/
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))
));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
use Symfony\Component\Validator\ValidatorInterface;
use Symfony\Component\Validator\Constraints\Valid;

/**
* Extension supporting the Symfony2 Validator component in forms.
*
* @author Bernhard Schussek <[email protected]>
*/
class ValidatorExtension extends AbstractExtension
{
private $validator;
Expand All @@ -25,6 +30,12 @@ public function __construct(ValidatorInterface $validator)
{
$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());
Expand Down
8 changes: 4 additions & 4 deletions src/Symfony/Component/Form/FormFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ public function __construct(FormRegistryInterface $registry, ResolvedFormTypeFac
/**
* {@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();
}

/**
* {@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();
}
Expand All @@ -61,7 +61,7 @@ public function createForProperty($class, $property, $data = null, array $option
/**
* {@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()
Expand All @@ -73,7 +73,7 @@ public function createBuilder($type, $data = null, array $options = array(), For
/**
* {@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;
Expand Down
Loading