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

Skip to content

Commit 87ccb6a

Browse files
committed
[Form] Added entry point "Forms" for more convenient usage outside of Symfony
1 parent cbd03ec commit 87ccb6a

22 files changed

+581
-48
lines changed

src/Symfony/Bundle/FrameworkBundle/Resources/config/templating_php.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<parameter key="templating.helper.session.class">Symfony\Bundle\FrameworkBundle\Templating\Helper\SessionHelper</parameter>
1515
<parameter key="templating.helper.code.class">Symfony\Bundle\FrameworkBundle\Templating\Helper\CodeHelper</parameter>
1616
<parameter key="templating.helper.translator.class">Symfony\Bundle\FrameworkBundle\Templating\Helper\TranslatorHelper</parameter>
17-
<parameter key="templating.helper.form.class">Symfony\Bundle\FrameworkBundle\Templating\Helper\FormHelper</parameter>
17+
<parameter key="templating.helper.form.class">Symfony\Component\Form\Extension\Templating\FormHelper</parameter>
1818
<parameter key="templating.form.engine.class">Symfony\Component\Form\Extension\Templating\TemplatingRendererEngine</parameter>
1919
<parameter key="templating.form.renderer.class">Symfony\Component\Form\FormRenderer</parameter>
2020
<parameter key="templating.globals.class">Symfony\Bundle\FrameworkBundle\Templating\GlobalVariables</parameter>

src/Symfony/Component/Form/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,3 +177,4 @@ CHANGELOG
177177
* made FormView properties public and deprecated their accessor methods
178178
* made the normalized data of a form accessible in the template through the variable "form.vars.data"
179179
* made the original data of a choice accessible in the template through the property "choice.data"
180+
* added convenience class Forms and FormFactoryBuilderInterface

src/Symfony/Component/Form/Extension/Csrf/CsrfExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class CsrfExtension extends AbstractExtension
2727
*
2828
* @param CsrfProviderInterface $csrfProvider The CSRF provider
2929
*/
30-
public function __construct(CsrfProviderInterface $csrfProvider)
30+
public function __construct(CsrfProviderInterface $csrfProvider = null)
3131
{
3232
$this->csrfProvider = $csrfProvider;
3333
}

src/Symfony/Bundle/FrameworkBundle/Templating/Helper/FormHelper.php renamed to src/Symfony/Component/Form/Extension/Templating/FormHelper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
namespace Symfony\Bundle\FrameworkBundle\Templating\Helper;
12+
namespace Symfony\Component\Form\Extension\Templating;
1313

1414
use Symfony\Component\Templating\Helper\Helper;
1515
use Symfony\Component\Form\FormRendererInterface;

src/Symfony/Component/Form/Extension/Validator/ValidatorExtension.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717
use Symfony\Component\Validator\ValidatorInterface;
1818
use Symfony\Component\Validator\Constraints\Valid;
1919

20+
/**
21+
* Extension supporting the Symfony2 Validator component in forms.
22+
*
23+
* @author Bernhard Schussek <[email protected]>
24+
*/
2025
class ValidatorExtension extends AbstractExtension
2126
{
2227
private $validator;
@@ -25,6 +30,12 @@ public function __construct(ValidatorInterface $validator)
2530
{
2631
$this->validator = $validator;
2732

33+
// Register the form constraints in the validator programmatically.
34+
// This functionality is required when using the Form component without
35+
// the DIC, where the XML file is loaded automatically. Thus the following
36+
// code must be kept synchronized with validation.xml
37+
38+
/** @var \Symfony\Component\Validator\Mapping\ClassMetadata $metadata */
2839
$metadata = $this->validator->getMetadataFactory()->getClassMetadata('Symfony\Component\Form\Form');
2940
$metadata->addConstraint(new Form());
3041
$metadata->addPropertyConstraint('children', new Valid());

src/Symfony/Component/Form/FormFactory.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,15 @@ public function __construct(FormRegistryInterface $registry, ResolvedFormTypeFac
3737
/**
3838
* {@inheritdoc}
3939
*/
40-
public function create($type, $data = null, array $options = array(), FormBuilderInterface $parent = null)
40+
public function create($type = 'form', $data = null, array $options = array(), FormBuilderInterface $parent = null)
4141
{
4242
return $this->createBuilder($type, $data, $options, $parent)->getForm();
4343
}
4444

4545
/**
4646
* {@inheritdoc}
4747
*/
48-
public function createNamed($name, $type, $data = null, array $options = array(), FormBuilderInterface $parent = null)
48+
public function createNamed($name, $type = 'form', $data = null, array $options = array(), FormBuilderInterface $parent = null)
4949
{
5050
return $this->createNamedBuilder($name, $type, $data, $options, $parent)->getForm();
5151
}
@@ -61,7 +61,7 @@ public function createForProperty($class, $property, $data = null, array $option
6161
/**
6262
* {@inheritdoc}
6363
*/
64-
public function createBuilder($type, $data = null, array $options = array(), FormBuilderInterface $parent = null)
64+
public function createBuilder($type = 'form', $data = null, array $options = array(), FormBuilderInterface $parent = null)
6565
{
6666
$name = $type instanceof FormTypeInterface || $type instanceof ResolvedFormTypeInterface
6767
? $type->getName()
@@ -73,7 +73,7 @@ public function createBuilder($type, $data = null, array $options = array(), For
7373
/**
7474
* {@inheritdoc}
7575
*/
76-
public function createNamedBuilder($name, $type, $data = null, array $options = array(), FormBuilderInterface $parent = null)
76+
public function createNamedBuilder($name, $type = 'form', $data = null, array $options = array(), FormBuilderInterface $parent = null)
7777
{
7878
if (null !== $data && !array_key_exists('data', $options)) {
7979
$options['data'] = $data;
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Form;
13+
14+
/**
15+
* @author Bernhard Schussek <[email protected]>
16+
*/
17+
18+
/**
19+
* The default implementation of FormFactoryBuilderInterface.
20+
*
21+
* @author Bernhard Schussek <[email protected]>
22+
*/
23+
class FormFactoryBuilder implements FormFactoryBuilderInterface
24+
{
25+
/**
26+
* @var ResolvedFormTypeFactoryInterface
27+
*/
28+
private $resolvedTypeFactory;
29+
30+
/**
31+
* @var array
32+
*/
33+
private $extensions = array();
34+
35+
/**
36+
* @var array
37+
*/
38+
private $types = array();
39+
40+
/**
41+
* @var array
42+
*/
43+
private $typeExtensions = array();
44+
45+
/**
46+
* @var array
47+
*/
48+
private $typeGuessers = array();
49+
50+
/**
51+
* {@inheritdoc}
52+
*/
53+
public function setResolvedTypeFactory(ResolvedFormTypeFactoryInterface $resolvedTypeFactory)
54+
{
55+
$this->resolvedTypeFactory = $resolvedTypeFactory;
56+
57+
return $this;
58+
}
59+
60+
/**
61+
* {@inheritdoc}
62+
*/
63+
public function addExtension(FormExtensionInterface $extension)
64+
{
65+
$this->extensions[] = $extension;
66+
67+
return $this;
68+
}
69+
70+
/**
71+
* {@inheritdoc}
72+
*/
73+
public function addExtensions(array $extensions)
74+
{
75+
$this->extensions = array_merge($this->extensions, $extensions);
76+
77+
return $this;
78+
}
79+
80+
/**
81+
* {@inheritdoc}
82+
*/
83+
public function addType(FormTypeInterface $type)
84+
{
85+
$this->types[$type->getName()] = $type;
86+
87+
return $this;
88+
}
89+
90+
/**
91+
* {@inheritdoc}
92+
*/
93+
public function addTypes(array $types)
94+
{
95+
foreach ($types as $type) {
96+
$this->types[$type->getName()] = $type;
97+
}
98+
99+
return $this;
100+
}
101+
102+
/**
103+
* {@inheritdoc}
104+
*/
105+
public function addTypeExtension(FormTypeExtensionInterface $typeExtension)
106+
{
107+
$this->typeExtensions[$typeExtension->getExtendedType()][] = $typeExtension;
108+
109+
return $this;
110+
}
111+
112+
/**
113+
* {@inheritdoc}
114+
*/
115+
public function addTypeExtensions(array $typeExtensions)
116+
{
117+
foreach ($typeExtensions as $typeExtension) {
118+
$this->typeExtensions[$typeExtension->getExtendedType()][] = $typeExtension;
119+
}
120+
121+
return $this;
122+
}
123+
124+
/**
125+
* {@inheritdoc}
126+
*/
127+
public function addTypeGuesser(FormTypeGuesserInterface $typeGuesser)
128+
{
129+
$this->typeGuessers[] = $typeGuesser;
130+
131+
return $this;
132+
}
133+
134+
/**
135+
* {@inheritdoc}
136+
*/
137+
public function addTypeGuessers(array $typeGuessers)
138+
{
139+
$this->typeGuessers = array_merge($this->typeGuessers, $typeGuessers);
140+
141+
return $this;
142+
}
143+
144+
/**
145+
* {@inheritdoc}
146+
*/
147+
public function getFormFactory()
148+
{
149+
$extensions = $this->extensions;
150+
151+
if (count($this->types) > 0 || count($this->typeExtensions) > 0 || count($this->typeGuessers) > 0) {
152+
$typeGuesser = count($this->typeGuessers) > 1
153+
? new FormTypeGuesserChain($this->typeGuessers)
154+
: $this->typeGuessers[0];
155+
156+
$extensions[] = new PreloadedExtension($this->types, $this->typeExtensions, $typeGuesser);
157+
}
158+
159+
$resolvedTypeFactory = $this->resolvedTypeFactory ?: new ResolvedFormTypeFactory();
160+
$registry = new FormRegistry($extensions, $resolvedTypeFactory);
161+
162+
return new FormFactory($registry, $resolvedTypeFactory);
163+
}
164+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Form;
13+
14+
/**
15+
* A builder for FormFactoryInterface objects.
16+
*
17+
* @author Bernhard Schussek <[email protected]>
18+
*/
19+
interface FormFactoryBuilderInterface
20+
{
21+
/**
22+
* Sets the factory for creating ResolvedFormTypeInterface instances.
23+
*
24+
* @param ResolvedFormTypeFactoryInterface $resolvedTypeFactory
25+
*
26+
* @return FormFactoryBuilderInterface The builder.
27+
*/
28+
public function setResolvedTypeFactory(ResolvedFormTypeFactoryInterface $resolvedTypeFactory);
29+
30+
/**
31+
* Adds an extension to be loaded by the factory.
32+
*
33+
* @param FormExtensionInterface $extension The extension.
34+
*
35+
* @return FormFactoryBuilderInterface The builder.
36+
*/
37+
public function addExtension(FormExtensionInterface $extension);
38+
39+
/**
40+
* Adds a list of extensions to be loaded by the factory.
41+
*
42+
* @param array $extensions The extensions.
43+
*
44+
* @return FormFactoryBuilderInterface The builder.
45+
*/
46+
public function addExtensions(array $extensions);
47+
48+
/**
49+
* Adds a form type to the factory.
50+
*
51+
* @param FormTypeInterface $type The form type.
52+
*
53+
* @return FormFactoryBuilderInterface The builder.
54+
*/
55+
public function addType(FormTypeInterface $type);
56+
57+
/**
58+
* Adds a list of form types to the factory.
59+
*
60+
* @param array $types The form types.
61+
*
62+
* @return FormFactoryBuilderInterface The builder.
63+
*/
64+
public function addTypes(array $types);
65+
66+
/**
67+
* Adds a form type extension to the factory.
68+
*
69+
* @param FormTypeExtensionInterface $typeExtension The form type extension.
70+
*
71+
* @return FormFactoryBuilderInterface The builder.
72+
*/
73+
public function addTypeExtension(FormTypeExtensionInterface $typeExtension);
74+
75+
/**
76+
* Adds a list of form type extensions to the factory.
77+
*
78+
* @param array $typeExtensions The form type extensions.
79+
*
80+
* @return FormFactoryBuilderInterface The builder.
81+
*/
82+
public function addTypeExtensions(array $typeExtensions);
83+
84+
/**
85+
* Adds a type guesser to the factory.
86+
*
87+
* @param FormTypeGuesserInterface $typeGuesser The type guesser.
88+
*
89+
* @return FormFactoryBuilderInterface The builder.
90+
*/
91+
public function addTypeGuesser(FormTypeGuesserInterface $typeGuesser);
92+
93+
/**
94+
* Adds a list of type guessers to the factory.
95+
*
96+
* @param array $typeGuessers The type guessers.
97+
*
98+
* @return FormFactoryBuilderInterface The builder.
99+
*/
100+
public function addTypeGuessers(array $typeGuessers);
101+
102+
/**
103+
* Builds and returns the factory.
104+
*
105+
* @return FormFactoryInterface The form factory.
106+
*/
107+
public function getFormFactory();
108+
}

0 commit comments

Comments
 (0)