[Form] Added entry point "Forms" for more convenient usage outside of Symfony
This commit is contained in:
parent
cbd03ec4c6
commit
87ccb6adb9
@ -14,7 +14,7 @@
|
|||||||
<parameter key="templating.helper.session.class">Symfony\Bundle\FrameworkBundle\Templating\Helper\SessionHelper</parameter>
|
<parameter key="templating.helper.session.class">Symfony\Bundle\FrameworkBundle\Templating\Helper\SessionHelper</parameter>
|
||||||
<parameter key="templating.helper.code.class">Symfony\Bundle\FrameworkBundle\Templating\Helper\CodeHelper</parameter>
|
<parameter key="templating.helper.code.class">Symfony\Bundle\FrameworkBundle\Templating\Helper\CodeHelper</parameter>
|
||||||
<parameter key="templating.helper.translator.class">Symfony\Bundle\FrameworkBundle\Templating\Helper\TranslatorHelper</parameter>
|
<parameter key="templating.helper.translator.class">Symfony\Bundle\FrameworkBundle\Templating\Helper\TranslatorHelper</parameter>
|
||||||
<parameter key="templating.helper.form.class">Symfony\Bundle\FrameworkBundle\Templating\Helper\FormHelper</parameter>
|
<parameter key="templating.helper.form.class">Symfony\Component\Form\Extension\Templating\FormHelper</parameter>
|
||||||
<parameter key="templating.form.engine.class">Symfony\Component\Form\Extension\Templating\TemplatingRendererEngine</parameter>
|
<parameter key="templating.form.engine.class">Symfony\Component\Form\Extension\Templating\TemplatingRendererEngine</parameter>
|
||||||
<parameter key="templating.form.renderer.class">Symfony\Component\Form\FormRenderer</parameter>
|
<parameter key="templating.form.renderer.class">Symfony\Component\Form\FormRenderer</parameter>
|
||||||
<parameter key="templating.globals.class">Symfony\Bundle\FrameworkBundle\Templating\GlobalVariables</parameter>
|
<parameter key="templating.globals.class">Symfony\Bundle\FrameworkBundle\Templating\GlobalVariables</parameter>
|
||||||
|
@ -177,3 +177,4 @@ CHANGELOG
|
|||||||
* made FormView properties public and deprecated their accessor methods
|
* 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 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"
|
* made the original data of a choice accessible in the template through the property "choice.data"
|
||||||
|
* added convenience class Forms and FormFactoryBuilderInterface
|
||||||
|
@ -27,7 +27,7 @@ class CsrfExtension extends AbstractExtension
|
|||||||
*
|
*
|
||||||
* @param CsrfProviderInterface $csrfProvider The CSRF provider
|
* @param CsrfProviderInterface $csrfProvider The CSRF provider
|
||||||
*/
|
*/
|
||||||
public function __construct(CsrfProviderInterface $csrfProvider)
|
public function __construct(CsrfProviderInterface $csrfProvider = null)
|
||||||
{
|
{
|
||||||
$this->csrfProvider = $csrfProvider;
|
$this->csrfProvider = $csrfProvider;
|
||||||
}
|
}
|
||||||
|
@ -9,7 +9,7 @@
|
|||||||
* file that was distributed with this source code.
|
* file that was distributed with this source code.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
namespace Symfony\Bundle\FrameworkBundle\Templating\Helper;
|
namespace Symfony\Component\Form\Extension\Templating;
|
||||||
|
|
||||||
use Symfony\Component\Templating\Helper\Helper;
|
use Symfony\Component\Templating\Helper\Helper;
|
||||||
use Symfony\Component\Form\FormRendererInterface;
|
use Symfony\Component\Form\FormRendererInterface;
|
@ -17,6 +17,11 @@ use Symfony\Component\Form\AbstractExtension;
|
|||||||
use Symfony\Component\Validator\ValidatorInterface;
|
use Symfony\Component\Validator\ValidatorInterface;
|
||||||
use Symfony\Component\Validator\Constraints\Valid;
|
use Symfony\Component\Validator\Constraints\Valid;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Extension supporting the Symfony2 Validator component in forms.
|
||||||
|
*
|
||||||
|
* @author Bernhard Schussek <bschussek@gmail.com>
|
||||||
|
*/
|
||||||
class ValidatorExtension extends AbstractExtension
|
class ValidatorExtension extends AbstractExtension
|
||||||
{
|
{
|
||||||
private $validator;
|
private $validator;
|
||||||
@ -25,6 +30,12 @@ class ValidatorExtension extends AbstractExtension
|
|||||||
{
|
{
|
||||||
$this->validator = $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 = $this->validator->getMetadataFactory()->getClassMetadata('Symfony\Component\Form\Form');
|
||||||
$metadata->addConstraint(new Form());
|
$metadata->addConstraint(new Form());
|
||||||
$metadata->addPropertyConstraint('children', new Valid());
|
$metadata->addPropertyConstraint('children', new Valid());
|
||||||
|
@ -37,7 +37,7 @@ class FormFactory implements FormFactoryInterface
|
|||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@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();
|
return $this->createBuilder($type, $data, $options, $parent)->getForm();
|
||||||
}
|
}
|
||||||
@ -45,7 +45,7 @@ class FormFactory implements FormFactoryInterface
|
|||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@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();
|
return $this->createNamedBuilder($name, $type, $data, $options, $parent)->getForm();
|
||||||
}
|
}
|
||||||
@ -61,7 +61,7 @@ class FormFactory implements FormFactoryInterface
|
|||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@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
|
$name = $type instanceof FormTypeInterface || $type instanceof ResolvedFormTypeInterface
|
||||||
? $type->getName()
|
? $type->getName()
|
||||||
@ -73,7 +73,7 @@ class FormFactory implements FormFactoryInterface
|
|||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@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)) {
|
if (null !== $data && !array_key_exists('data', $options)) {
|
||||||
$options['data'] = $data;
|
$options['data'] = $data;
|
||||||
|
164
src/Symfony/Component/Form/FormFactoryBuilder.php
Normal file
164
src/Symfony/Component/Form/FormFactoryBuilder.php
Normal file
@ -0,0 +1,164 @@
|
|||||||
|
<?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;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Bernhard Schussek <bschussek@gmail.com>
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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);
|
||||||
|
}
|
||||||
|
}
|
108
src/Symfony/Component/Form/FormFactoryBuilderInterface.php
Normal file
108
src/Symfony/Component/Form/FormFactoryBuilderInterface.php
Normal 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();
|
||||||
|
}
|
@ -30,7 +30,7 @@ interface FormFactoryInterface
|
|||||||
*
|
*
|
||||||
* @throws Exception\FormException if any given option is not applicable to the given type
|
* @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.
|
* Returns a form.
|
||||||
@ -47,7 +47,7 @@ interface FormFactoryInterface
|
|||||||
*
|
*
|
||||||
* @throws Exception\FormException if any given option is not applicable to the given type
|
* @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.
|
* 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
|
* @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.
|
* Returns a form builder.
|
||||||
@ -93,7 +93,7 @@ interface FormFactoryInterface
|
|||||||
*
|
*
|
||||||
* @throws Exception\FormException if any given option is not applicable to the given type
|
* @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.
|
* Returns a form builder for a property of a class.
|
||||||
|
138
src/Symfony/Component/Form/Forms.php
Normal file
138
src/Symfony/Component/Form/Forms.php
Normal file
@ -0,0 +1,138 @@
|
|||||||
|
<?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\ValidatorFactory;
|
||||||
|
* use Symfony\Component\Form\Extension\Validator\ValidatorExtension;
|
||||||
|
*
|
||||||
|
* $validator = ValidatorFactory::buildDefault()->getValidator();
|
||||||
|
* $formFactory = Forms::createFormFactoryBuilder()
|
||||||
|
* ->addExtension(new ValidatorExtension($validator))
|
||||||
|
* ->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()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
100
src/Symfony/Component/Form/PreloadedExtension.php
Normal file
100
src/Symfony/Component/Form/PreloadedExtension.php
Normal file
@ -0,0 +1,100 @@
|
|||||||
|
<?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;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Bernhard Schussek <bschussek@gmail.com>
|
||||||
|
*/
|
||||||
|
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 isset($this->typeExtensions[$name]) && count($this->typeExtensions[$name]) > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function getTypeGuesser()
|
||||||
|
{
|
||||||
|
return $this->typeGuesser;
|
||||||
|
}
|
||||||
|
}
|
@ -39,7 +39,6 @@ abstract class AbstractLayoutTest extends FormIntegrationTestCase
|
|||||||
protected function getExtensions()
|
protected function getExtensions()
|
||||||
{
|
{
|
||||||
return array(
|
return array(
|
||||||
new CoreExtension(),
|
|
||||||
new CsrfExtension($this->csrfProvider),
|
new CsrfExtension($this->csrfProvider),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -9,7 +9,7 @@
|
|||||||
* file that was distributed with this source code.
|
* file that was distributed with this source code.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
namespace Symfony\Bundle\FrameworkBundle\Tests\Templating\Helper\Fixtures;
|
namespace Symfony\Component\Form\Tests\Extension\Templating\Fixtures;
|
||||||
|
|
||||||
use Symfony\Component\Templating\TemplateNameParserInterface;
|
use Symfony\Component\Templating\TemplateNameParserInterface;
|
||||||
use Symfony\Component\Templating\TemplateReference;
|
use Symfony\Component\Templating\TemplateReference;
|
@ -9,7 +9,7 @@
|
|||||||
* file that was distributed with this source code.
|
* file that was distributed with this source code.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
namespace Symfony\Bundle\FrameworkBundle\Tests\Templating\Helper\Fixtures;
|
namespace Symfony\Component\Form\Tests\Extension\Templating\Fixtures;
|
||||||
|
|
||||||
use Symfony\Component\Translation\TranslatorInterface;
|
use Symfony\Component\Translation\TranslatorInterface;
|
||||||
|
|
@ -9,18 +9,20 @@
|
|||||||
* file that was distributed with this source code.
|
* file that was distributed with this source code.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
namespace Symfony\Bundle\FrameworkBundle\Tests\Templating\Helper;
|
namespace Symfony\Component\Form\Tests\Extension\Templating;
|
||||||
|
|
||||||
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\Templating\PhpEngine;
|
|
||||||
use Symfony\Component\Templating\Loader\FilesystemLoader;
|
|
||||||
use Symfony\Component\Form\FormView;
|
use Symfony\Component\Form\FormView;
|
||||||
use Symfony\Component\Form\FormRenderer;
|
use Symfony\Component\Form\FormRenderer;
|
||||||
|
use Symfony\Component\Form\Extension\Templating\FormHelper;
|
||||||
use Symfony\Component\Form\Extension\Templating\TemplatingRendererEngine;
|
use Symfony\Component\Form\Extension\Templating\TemplatingRendererEngine;
|
||||||
use Symfony\Component\Form\Tests\AbstractDivLayoutTest;
|
use Symfony\Component\Form\Tests\AbstractDivLayoutTest;
|
||||||
|
use Symfony\Component\Form\Tests\Extension\Templating\Fixtures\StubTemplateNameParser;
|
||||||
|
use Symfony\Component\Form\Tests\Extension\Templating\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 FormHelperDivLayoutTest extends AbstractDivLayoutTest
|
class FormHelperDivLayoutTest extends AbstractDivLayoutTest
|
||||||
{
|
{
|
||||||
@ -28,9 +30,20 @@ class FormHelperDivLayoutTest extends AbstractDivLayoutTest
|
|||||||
|
|
||||||
protected function setUp()
|
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();
|
parent::setUp();
|
||||||
|
|
||||||
$root = realpath(__DIR__.'/../../../Resources/views');
|
// 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');
|
$rootTheme = realpath(__DIR__.'/Resources');
|
||||||
$templateNameParser = new StubTemplateNameParser($root, $rootTheme);
|
$templateNameParser = new StubTemplateNameParser($root, $rootTheme);
|
||||||
$loader = new FilesystemLoader(array());
|
$loader = new FilesystemLoader(array());
|
@ -9,28 +9,41 @@
|
|||||||
* file that was distributed with this source code.
|
* file that was distributed with this source code.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
namespace Symfony\Bundle\FrameworkBundle\Tests\Templating\Helper;
|
namespace Symfony\Component\Form\Tests\Extension\Templating;
|
||||||
|
|
||||||
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\FormView;
|
||||||
use Symfony\Component\Form\FormRenderer;
|
use Symfony\Component\Form\FormRenderer;
|
||||||
|
use Symfony\Component\Form\Extension\Templating\FormHelper;
|
||||||
use Symfony\Component\Form\Extension\Templating\TemplatingRendererEngine;
|
use Symfony\Component\Form\Extension\Templating\TemplatingRendererEngine;
|
||||||
use Symfony\Component\Form\Tests\AbstractTableLayoutTest;
|
use Symfony\Component\Form\Tests\AbstractTableLayoutTest;
|
||||||
|
use Symfony\Component\Form\Tests\Extension\Templating\Fixtures\StubTemplateNameParser;
|
||||||
|
use Symfony\Component\Form\Tests\Extension\Templating\Fixtures\StubTranslator;
|
||||||
use Symfony\Component\Templating\PhpEngine;
|
use Symfony\Component\Templating\PhpEngine;
|
||||||
use Symfony\Component\Templating\Loader\FilesystemLoader;
|
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
|
class FormHelperTableLayoutTest extends AbstractTableLayoutTest
|
||||||
{
|
{
|
||||||
protected $helper;
|
protected $helper;
|
||||||
|
|
||||||
protected function setUp()
|
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();
|
parent::setUp();
|
||||||
|
|
||||||
$root = realpath(__DIR__.'/../../../Resources/views');
|
// 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');
|
$rootTheme = realpath(__DIR__.'/Resources');
|
||||||
$templateNameParser = new StubTemplateNameParser($root, $rootTheme);
|
$templateNameParser = new StubTemplateNameParser($root, $rootTheme);
|
||||||
$loader = new FilesystemLoader(array());
|
$loader = new FilesystemLoader(array());
|
@ -11,9 +11,7 @@
|
|||||||
|
|
||||||
namespace Symfony\Component\Form\Tests;
|
namespace Symfony\Component\Form\Tests;
|
||||||
|
|
||||||
use Symfony\Component\Form\FormFactory;
|
use Symfony\Component\Form\Forms;
|
||||||
use Symfony\Component\Form\ResolvedFormTypeFactory;
|
|
||||||
use Symfony\Component\Form\FormRegistry;
|
|
||||||
use Symfony\Component\Form\Extension\Core\CoreExtension;
|
use Symfony\Component\Form\Extension\Core\CoreExtension;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -22,17 +20,7 @@ use Symfony\Component\Form\Extension\Core\CoreExtension;
|
|||||||
class FormIntegrationTestCase extends \PHPUnit_Framework_TestCase
|
class FormIntegrationTestCase extends \PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @var ResolvedFormTypeFactory
|
* @var \Symfony\Component\Form\FormFactoryInterface
|
||||||
*/
|
|
||||||
protected $resolvedTypeFactory;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var FormRegistry
|
|
||||||
*/
|
|
||||||
protected $registry;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var FormFactory
|
|
||||||
*/
|
*/
|
||||||
protected $factory;
|
protected $factory;
|
||||||
|
|
||||||
@ -42,15 +30,13 @@ class FormIntegrationTestCase extends \PHPUnit_Framework_TestCase
|
|||||||
$this->markTestSkipped('The "EventDispatcher" component is not available');
|
$this->markTestSkipped('The "EventDispatcher" component is not available');
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->resolvedTypeFactory = new ResolvedFormTypeFactory();
|
$this->factory = Forms::createFormFactoryBuilder()
|
||||||
$this->registry = new FormRegistry($this->getExtensions(), $this->resolvedTypeFactory);
|
->addExtensions($this->getExtensions())
|
||||||
$this->factory = new FormFactory($this->registry, $this->resolvedTypeFactory);
|
->getFormFactory();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function getExtensions()
|
protected function getExtensions()
|
||||||
{
|
{
|
||||||
return array(
|
return array();
|
||||||
new CoreExtension(),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user