2011-02-19 12:52:37 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
2011-04-24 12:59:46 +01:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2011-02-19 12:52:37 +00:00
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Symfony\Component\Form;
|
|
|
|
|
2011-04-24 20:56:06 +01:00
|
|
|
use Symfony\Component\Form\Exception\UnexpectedTypeException;
|
2011-02-19 12:52:37 +00:00
|
|
|
|
2011-03-02 11:20:30 +00:00
|
|
|
class FormFactory implements FormFactoryInterface
|
2011-02-19 12:52:37 +00:00
|
|
|
{
|
2011-05-10 17:23:58 +01:00
|
|
|
/**
|
2012-07-12 18:17:14 +01:00
|
|
|
* @var FormRegistryInterface
|
2011-05-10 17:23:58 +01:00
|
|
|
*/
|
2012-07-12 18:17:14 +01:00
|
|
|
private $registry;
|
2011-03-02 13:58:19 +00:00
|
|
|
|
2012-07-29 18:10:47 +01:00
|
|
|
/**
|
|
|
|
* @var ResolvedFormTypeFactoryInterface
|
|
|
|
*/
|
|
|
|
private $resolvedTypeFactory;
|
|
|
|
|
|
|
|
public function __construct(FormRegistryInterface $registry, ResolvedFormTypeFactoryInterface $resolvedTypeFactory)
|
2011-02-19 12:52:37 +00:00
|
|
|
{
|
2012-07-12 18:17:14 +01:00
|
|
|
$this->registry = $registry;
|
2012-07-29 18:10:47 +01:00
|
|
|
$this->resolvedTypeFactory = $resolvedTypeFactory;
|
2011-04-22 18:22:26 +01:00
|
|
|
}
|
|
|
|
|
2011-05-10 17:23:58 +01:00
|
|
|
/**
|
2012-05-21 03:26:59 +01:00
|
|
|
* {@inheritdoc}
|
2011-05-10 17:23:58 +01:00
|
|
|
*/
|
2015-06-23 17:42:21 +01:00
|
|
|
public function create($type = 'Symfony\Component\Form\Extension\Core\Type\FormType', $data = null, array $options = array())
|
2011-02-19 14:26:07 +00:00
|
|
|
{
|
2013-04-13 14:16:29 +01:00
|
|
|
return $this->createBuilder($type, $data, $options)->getForm();
|
2011-04-22 09:42:21 +01:00
|
|
|
}
|
|
|
|
|
2011-05-10 17:23:58 +01:00
|
|
|
/**
|
2012-05-21 03:26:59 +01:00
|
|
|
* {@inheritdoc}
|
2011-05-10 17:23:58 +01:00
|
|
|
*/
|
2015-06-23 17:42:21 +01:00
|
|
|
public function createNamed($name, $type = 'Symfony\Component\Form\Extension\Core\Type\FormType', $data = null, array $options = array())
|
2011-04-22 09:42:21 +01:00
|
|
|
{
|
2013-04-13 14:16:29 +01:00
|
|
|
return $this->createNamedBuilder($name, $type, $data, $options)->getForm();
|
2011-04-22 09:42:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-05-21 03:26:59 +01:00
|
|
|
* {@inheritdoc}
|
2011-04-22 09:42:21 +01:00
|
|
|
*/
|
2013-04-13 14:16:29 +01:00
|
|
|
public function createForProperty($class, $property, $data = null, array $options = array())
|
2011-04-22 09:42:21 +01:00
|
|
|
{
|
2013-04-13 14:16:29 +01:00
|
|
|
return $this->createBuilderForProperty($class, $property, $data, $options)->getForm();
|
2011-04-22 09:42:21 +01:00
|
|
|
}
|
|
|
|
|
2011-05-10 17:23:58 +01:00
|
|
|
/**
|
2012-05-21 03:26:59 +01:00
|
|
|
* {@inheritdoc}
|
2011-05-10 17:23:58 +01:00
|
|
|
*/
|
2015-06-23 17:42:21 +01:00
|
|
|
public function createBuilder($type = 'Symfony\Component\Form\Extension\Core\Type\FormType', $data = null, array $options = array())
|
2011-04-22 09:42:21 +01:00
|
|
|
{
|
2015-10-02 13:08:08 +01:00
|
|
|
if (!is_string($type)) {
|
|
|
|
throw new UnexpectedTypeException($type, 'string');
|
2015-06-23 17:42:21 +01:00
|
|
|
}
|
2011-04-22 09:42:21 +01:00
|
|
|
|
2015-11-30 16:36:51 +00:00
|
|
|
return $this->createNamedBuilder($this->registry->getType($type)->getBlockPrefix(), $type, $data, $options);
|
2011-04-22 09:42:21 +01:00
|
|
|
}
|
2011-03-04 15:01:50 +00:00
|
|
|
|
2011-05-10 17:23:58 +01:00
|
|
|
/**
|
2012-05-21 03:26:59 +01:00
|
|
|
* {@inheritdoc}
|
2011-05-10 17:23:58 +01:00
|
|
|
*/
|
2015-06-23 17:42:21 +01:00
|
|
|
public function createNamedBuilder($name, $type = 'Symfony\Component\Form\Extension\Core\Type\FormType', $data = null, array $options = array())
|
2011-04-22 09:42:21 +01:00
|
|
|
{
|
2012-07-13 11:12:25 +01:00
|
|
|
if (null !== $data && !array_key_exists('data', $options)) {
|
2011-04-24 12:32:29 +01:00
|
|
|
$options['data'] = $data;
|
|
|
|
}
|
|
|
|
|
2015-10-01 08:27:49 +01:00
|
|
|
if (!is_string($type)) {
|
|
|
|
throw new UnexpectedTypeException($type, 'string');
|
2011-05-13 17:29:18 +01:00
|
|
|
}
|
2011-02-19 16:30:19 +00:00
|
|
|
|
2015-10-01 08:27:49 +01:00
|
|
|
$type = $this->registry->getType($type);
|
|
|
|
|
2013-09-17 10:39:17 +01:00
|
|
|
$builder = $type->createBuilder($this, $name, $options);
|
|
|
|
|
|
|
|
// Explicitly call buildForm() in order to be able to override either
|
|
|
|
// createBuilder() or buildForm() in the resolved form type
|
|
|
|
$type->buildForm($builder, $builder->getOptions());
|
|
|
|
|
|
|
|
return $builder;
|
2011-03-18 11:50:26 +00:00
|
|
|
}
|
|
|
|
|
2011-05-10 17:23:58 +01:00
|
|
|
/**
|
2012-05-21 03:26:59 +01:00
|
|
|
* {@inheritdoc}
|
2011-05-10 17:23:58 +01:00
|
|
|
*/
|
2013-04-13 14:16:29 +01:00
|
|
|
public function createBuilderForProperty($class, $property, $data = null, array $options = array())
|
2011-03-02 13:58:19 +00:00
|
|
|
{
|
2012-11-08 18:49:46 +00:00
|
|
|
if (null === $guesser = $this->registry->getTypeGuesser()) {
|
2015-06-23 17:42:21 +01:00
|
|
|
return $this->createNamedBuilder($property, 'Symfony\Component\Form\Extension\Core\Type\TextType', $data, $options);
|
2012-11-08 18:49:46 +00:00
|
|
|
}
|
|
|
|
|
2012-07-12 18:17:14 +01:00
|
|
|
$typeGuess = $guesser->guessType($class, $property);
|
|
|
|
$maxLengthGuess = $guesser->guessMaxLength($class, $property);
|
|
|
|
$requiredGuess = $guesser->guessRequired($class, $property);
|
|
|
|
$patternGuess = $guesser->guessPattern($class, $property);
|
2011-04-22 16:41:21 +01:00
|
|
|
|
2015-06-23 17:42:21 +01:00
|
|
|
$type = $typeGuess ? $typeGuess->getType() : 'Symfony\Component\Form\Extension\Core\Type\TextType';
|
2011-03-02 13:58:19 +00:00
|
|
|
|
2012-03-19 22:57:21 +00:00
|
|
|
$maxLength = $maxLengthGuess ? $maxLengthGuess->getValue() : null;
|
2014-10-22 19:27:13 +01:00
|
|
|
$pattern = $patternGuess ? $patternGuess->getValue() : null;
|
2012-03-19 22:57:21 +00:00
|
|
|
|
2012-04-23 14:55:54 +01:00
|
|
|
if (null !== $pattern) {
|
2014-11-23 03:56:17 +00:00
|
|
|
$options = array_replace_recursive(array('attr' => array('pattern' => $pattern)), $options);
|
2012-04-23 14:55:54 +01:00
|
|
|
}
|
|
|
|
|
2012-03-19 22:57:21 +00:00
|
|
|
if (null !== $maxLength) {
|
2014-11-23 03:56:17 +00:00
|
|
|
$options = array_replace_recursive(array('attr' => array('maxlength' => $maxLength)), $options);
|
2011-03-02 13:58:19 +00:00
|
|
|
}
|
|
|
|
|
2012-04-23 14:55:54 +01:00
|
|
|
if ($requiredGuess) {
|
|
|
|
$options = array_merge(array('required' => $requiredGuess->getValue()), $options);
|
2012-04-13 16:07:09 +01:00
|
|
|
}
|
|
|
|
|
2011-03-02 13:58:19 +00:00
|
|
|
// user options may override guessed options
|
2011-03-18 15:20:00 +00:00
|
|
|
if ($typeGuess) {
|
|
|
|
$options = array_merge($typeGuess->getOptions(), $options);
|
2011-03-18 13:37:54 +00:00
|
|
|
}
|
2011-03-02 13:58:19 +00:00
|
|
|
|
2013-04-13 14:16:29 +01:00
|
|
|
return $this->createNamedBuilder($property, $type, $data, $options);
|
2011-03-02 13:58:19 +00:00
|
|
|
}
|
2011-03-24 21:20:54 +00:00
|
|
|
}
|