[Form] Added first version of FormFactory, ValuePlugin and ParentNamePlugin
This commit is contained in:
parent
f7dc71ef59
commit
61ea51d858
@ -15,6 +15,7 @@ use Symfony\Component\Form\ValueTransformer\ValueTransformerInterface;
|
||||
use Symfony\Component\Form\ValueTransformer\TransformationFailedException;
|
||||
use Symfony\Component\Form\DataProcessor\DataProcessorInterface;
|
||||
use Symfony\Component\Form\Renderer\RendererInterface;
|
||||
use Symfony\Component\Form\Renderer\Plugin\PluginInterface;
|
||||
|
||||
/**
|
||||
* Base class for form fields
|
||||
@ -417,6 +418,8 @@ class Field extends Configurable implements FieldInterface
|
||||
protected function setNormalizationTransformer(ValueTransformerInterface $normalizationTransformer)
|
||||
{
|
||||
$this->normalizationTransformer = $normalizationTransformer;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -437,6 +440,8 @@ class Field extends Configurable implements FieldInterface
|
||||
protected function setValueTransformer(ValueTransformerInterface $valueTransformer)
|
||||
{
|
||||
$this->valueTransformer = $valueTransformer;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -457,6 +462,8 @@ class Field extends Configurable implements FieldInterface
|
||||
protected function setDataProcessor(DataProcessorInterface $dataProcessor)
|
||||
{
|
||||
$this->dataProcessor = $dataProcessor;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -477,6 +484,8 @@ class Field extends Configurable implements FieldInterface
|
||||
public function setRenderer(RendererInterface $renderer)
|
||||
{
|
||||
$this->renderer = $renderer;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -489,6 +498,13 @@ class Field extends Configurable implements FieldInterface
|
||||
return $this->renderer;
|
||||
}
|
||||
|
||||
public function addRendererPlugin(PluginInterface $plugin)
|
||||
{
|
||||
$this->renderer->addPlugin($plugin);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalizes the value if a normalization transformer is set
|
||||
*
|
||||
|
59
src/Symfony/Component/Form/FormFactory.php
Normal file
59
src/Symfony/Component/Form/FormFactory.php
Normal file
@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien.potencier@symfony-project.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\ValueTransformer\BooleanToStringTransformer;
|
||||
use Symfony\Component\Form\Renderer\DefaultRenderer;
|
||||
use Symfony\Component\Form\Renderer\Plugin\IdPlugin;
|
||||
use Symfony\Component\Form\Renderer\Plugin\NamePlugin;
|
||||
use Symfony\Component\Form\Renderer\Plugin\ValuePlugin;
|
||||
|
||||
class FormFactory
|
||||
{
|
||||
private $theme;
|
||||
|
||||
public function setTheme(ThemeInterface $theme)
|
||||
{
|
||||
$this->theme = $theme;
|
||||
}
|
||||
|
||||
public function getTheme()
|
||||
{
|
||||
return $this->theme;
|
||||
}
|
||||
|
||||
protected function getField($name, $template)
|
||||
{
|
||||
$field = new Field($name);
|
||||
|
||||
return $field
|
||||
->setRenderer(new DefaultRenderer($this->theme, $template))
|
||||
->addRendererPlugin(new IdPlugin($field))
|
||||
->addRendererPlugin(new NamePlugin($field));
|
||||
}
|
||||
|
||||
public function getCheckboxField($name, $value = '1')
|
||||
{
|
||||
return $this->getField($name, 'checkbox')
|
||||
->setValueTransformer(new BooleanToStringTransformer())
|
||||
->addRendererPlugin(new ValuePlugin($value));
|
||||
}
|
||||
|
||||
public function getRadioField($name)
|
||||
{
|
||||
$field = $this->getField($name, 'radio');
|
||||
|
||||
return $field
|
||||
->setValueTransformer(new BooleanToStringTransformer())
|
||||
->addRendererPlugin(new ParentNamePlugin($field));
|
||||
}
|
||||
}
|
@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
|
||||
*
|
||||
* For the full copyright and license infieldation, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\Form\Renderer\Plugin;
|
||||
|
||||
use Symfony\Component\Form\Renderer\RendererInterface;
|
||||
use Symfony\Component\Form\FieldInterface;
|
||||
|
||||
class ParentNamePlugin implements PluginInterface
|
||||
{
|
||||
private $field;
|
||||
|
||||
public function __construct(FieldInterface $field)
|
||||
{
|
||||
$this->field = $field;
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders the HTML enctype in the field tag, if necessary
|
||||
*
|
||||
* Example usage in Twig templates:
|
||||
*
|
||||
* <field action="..." method="post" {{ field.render.enctype }}>
|
||||
*
|
||||
* @param Form $field The field for which to render the encoding type
|
||||
*/
|
||||
public function setUp(RendererInterface $renderer)
|
||||
{
|
||||
if ($this->field->hasParent()) {
|
||||
$parentRenderer = $this->field->getParent()->getRenderer();
|
||||
$renderer->setParameter('name', $parentRenderer->getParameter('name'));
|
||||
}
|
||||
}
|
||||
}
|
31
src/Symfony/Component/Form/Renderer/Plugin/ValuePlugin.php
Normal file
31
src/Symfony/Component/Form/Renderer/Plugin/ValuePlugin.php
Normal file
@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
|
||||
*
|
||||
* For the full copyright and license infieldation, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\Form\Renderer\Plugin;
|
||||
|
||||
use Symfony\Component\Form\Renderer\RendererInterface;
|
||||
|
||||
class ValuePlugin implements PluginInterface
|
||||
{
|
||||
private $value;
|
||||
|
||||
public function __construct($value = null)
|
||||
{
|
||||
$this->value = $value;
|
||||
}
|
||||
|
||||
public function setUp(RendererInterface $renderer)
|
||||
{
|
||||
if (null !== $this->value) {
|
||||
$renderer->setParameter('value', $this->value);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user