4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Form
;
14 use Symfony\Component\Form\Exception\UnexpectedTypeException
;
16 class FormFactory
implements FormFactoryInterface
19 * @var FormRegistryInterface
24 * @var ResolvedFormTypeFactoryInterface
26 private $resolvedTypeFactory;
28 public function __construct(FormRegistryInterface
$registry, ResolvedFormTypeFactoryInterface
$resolvedTypeFactory)
30 $this->registry
= $registry;
31 $this->resolvedTypeFactory
= $resolvedTypeFactory;
37 public function create($type = 'form', $data = null, array $options = array())
39 return $this->createBuilder($type, $data, $options)->getForm();
45 public function createNamed($name, $type = 'form', $data = null, array $options = array())
47 return $this->createNamedBuilder($name, $type, $data, $options)->getForm();
53 public function createForProperty($class, $property, $data = null, array $options = array())
55 return $this->createBuilderForProperty($class, $property, $data, $options)->getForm();
61 public function createBuilder($type = 'form', $data = null, array $options = array())
63 $name = $type instanceof FormTypeInterface
|| $type instanceof ResolvedFormTypeInterface
67 return $this->createNamedBuilder($name, $type, $data, $options);
73 public function createNamedBuilder($name, $type = 'form', $data = null, array $options = array())
75 if (null !== $data && !array_key_exists('data', $options)) {
76 $options['data'] = $data;
79 if ($type instanceof FormTypeInterface
) {
80 $type = $this->resolveType($type);
81 } elseif (is_string($type)) {
82 $type = $this->registry
->getType($type);
83 } elseif (!$type instanceof ResolvedFormTypeInterface
) {
84 throw new UnexpectedTypeException($type, 'string, Symfony\Component\Form\ResolvedFormTypeInterface or Symfony\Component\Form\FormTypeInterface');
87 return $type->createBuilder($this, $name, $options);
93 public function createBuilderForProperty($class, $property, $data = null, array $options = array())
95 if (null === $guesser = $this->registry
->getTypeGuesser()) {
96 return $this->createNamedBuilder($property, 'text', $data, $options);
99 $typeGuess = $guesser->guessType($class, $property);
100 $maxLengthGuess = $guesser->guessMaxLength($class, $property);
101 $requiredGuess = $guesser->guessRequired($class, $property);
102 $patternGuess = $guesser->guessPattern($class, $property);
104 $type = $typeGuess ? $typeGuess->getType() : 'text';
106 $maxLength = $maxLengthGuess ? $maxLengthGuess->getValue() : null;
107 $pattern = $patternGuess ? $patternGuess->getValue() : null;
109 if (null !== $pattern) {
110 $options = array_merge(array('pattern' => $pattern), $options);
113 if (null !== $maxLength) {
114 $options = array_merge(array('max_length' => $maxLength), $options);
117 if ($requiredGuess) {
118 $options = array_merge(array('required' => $requiredGuess->getValue()), $options);
121 // user options may override guessed options
123 $options = array_merge($typeGuess->getOptions(), $options);
126 return $this->createNamedBuilder($property, $type, $data, $options);
130 * Wraps a type into a ResolvedFormTypeInterface implementation and connects
131 * it with its parent type.
133 * @param FormTypeInterface $type The type to resolve.
135 * @return ResolvedFormTypeInterface The resolved type.
137 private function resolveType(FormTypeInterface
$type)
139 $parentType = $type->getParent();
141 if ($parentType instanceof FormTypeInterface
) {
142 $parentType = $this->resolveType($parentType);
143 } elseif (null !== $parentType) {
144 $parentType = $this->registry
->getType($parentType);
147 return $this->resolvedTypeFactory
->createResolvedType(
149 // Type extensions are not supported for unregistered type instances,
150 // i.e. type instances that are passed to the FormFactory directly,
151 // nor for their parents, if getParent() also returns a type instance.