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\InvalidArgumentException
;
15 use Symfony\Component\Form\Exception\UnexpectedTypeException
;
16 use Symfony\Component\EventDispatcher\EventDispatcher
;
17 use Symfony\Component\OptionsResolver\OptionsResolver
;
20 * A wrapper for a form type and its extensions.
22 * @author Bernhard Schussek <bschussek@gmail.com>
24 class ResolvedFormType
implements ResolvedFormTypeInterface
27 * @var FormTypeInterface
34 private $typeExtensions;
37 * @var ResolvedFormTypeInterface
42 * @var OptionsResolver
44 private $optionsResolver;
46 public function __construct(FormTypeInterface
$innerType, array $typeExtensions = array(), ResolvedFormTypeInterface
$parent = null)
48 if (!preg_match('/^[a-z0-9_]*$/i', $innerType->getName())) {
49 throw new InvalidArgumentException(sprintf(
50 'The "%s" form type name ("%s") is not valid. Names must only contain letters, numbers, and "_".',
51 get_class($innerType),
56 foreach ($typeExtensions as $extension) {
57 if (!$extension instanceof FormTypeExtensionInterface
) {
58 throw new UnexpectedTypeException($extension, 'Symfony\Component\Form\FormTypeExtensionInterface');
62 $this->innerType
= $innerType;
63 $this->typeExtensions
= $typeExtensions;
64 $this->parent
= $parent;
70 public function getName()
72 return $this->innerType
->getName();
78 public function getParent()
86 public function getInnerType()
88 return $this->innerType
;
94 public function getTypeExtensions()
97 if ($this->innerType
instanceof AbstractType
) {
98 return $this->innerType
->getExtensions();
101 return $this->typeExtensions
;
107 public function createBuilder(FormFactoryInterface
$factory, $name, array $options = array())
109 $options = $this->getOptionsResolver()->resolve($options);
111 // Should be decoupled from the specific option at some point
112 $dataClass = isset($options['data_class']) ? $options['data_class'] : null;
114 $builder = $this->newBuilder($name, $dataClass, $factory, $options);
115 $builder->setType($this);
117 $this->buildForm($builder, $options);
125 public function createView(FormInterface
$form, FormView
$parent = null)
127 $options = $form->getConfig()->getOptions();
129 $view = $this->newView($parent);
131 $this->buildView($view, $form, $options);
133 foreach ($form as $name => $child) {
134 /* @var FormInterface $child */
135 $view->children
[$name] = $child->createView($view);
138 $this->finishView($view, $form, $options);
144 * Configures a form builder for the type hierarchy.
146 * This method is protected in order to allow implementing classes
147 * to change or call it in re-implementations of {@link createBuilder()}.
149 * @param FormBuilderInterface $builder The builder to configure.
150 * @param array $options The options used for the configuration.
152 public function buildForm(FormBuilderInterface
$builder, array $options)
154 if (null !== $this->parent
) {
155 $this->parent
->buildForm($builder, $options);
158 $this->innerType
->buildForm($builder, $options);
160 foreach ($this->typeExtensions
as $extension) {
161 /* @var FormTypeExtensionInterface $extension */
162 $extension->buildForm($builder, $options);
167 * Configures a form view for the type hierarchy.
169 * This method is protected in order to allow implementing classes
170 * to change or call it in re-implementations of {@link createView()}.
172 * It is called before the children of the view are built.
174 * @param FormView $view The form view to configure.
175 * @param FormInterface $form The form corresponding to the view.
176 * @param array $options The options used for the configuration.
178 public function buildView(FormView
$view, FormInterface
$form, array $options)
180 if (null !== $this->parent
) {
181 $this->parent
->buildView($view, $form, $options);
184 $this->innerType
->buildView($view, $form, $options);
186 foreach ($this->typeExtensions
as $extension) {
187 /* @var FormTypeExtensionInterface $extension */
188 $extension->buildView($view, $form, $options);
193 * Finishes a form view for the type hierarchy.
195 * This method is protected in order to allow implementing classes
196 * to change or call it in re-implementations of {@link createView()}.
198 * It is called after the children of the view have been built.
200 * @param FormView $view The form view to configure.
201 * @param FormInterface $form The form corresponding to the view.
202 * @param array $options The options used for the configuration.
204 public function finishView(FormView
$view, FormInterface
$form, array $options)
206 if (null !== $this->parent
) {
207 $this->parent
->finishView($view, $form, $options);
210 $this->innerType
->finishView($view, $form, $options);
212 foreach ($this->typeExtensions
as $extension) {
213 /* @var FormTypeExtensionInterface $extension */
214 $extension->finishView($view, $form, $options);
219 * Returns the configured options resolver used for this type.
221 * This method is protected in order to allow implementing classes
222 * to change or call it in re-implementations of {@link createBuilder()}.
224 * @return \Symfony\Component\OptionsResolver\OptionsResolverInterface The options resolver.
226 public function getOptionsResolver()
228 if (null === $this->optionsResolver
) {
229 if (null !== $this->parent
) {
230 $this->optionsResolver
= clone $this->parent
->getOptionsResolver();
232 $this->optionsResolver
= new OptionsResolver();
235 $this->innerType
->setDefaultOptions($this->optionsResolver
);
237 foreach ($this->typeExtensions
as $extension) {
238 /* @var FormTypeExtensionInterface $extension */
239 $extension->setDefaultOptions($this->optionsResolver
);
243 return $this->optionsResolver
;
247 * Creates a new builder instance.
249 * Override this method if you want to customize the builder class.
251 * @param string $name The name of the builder.
252 * @param string $dataClass The data class.
253 * @param FormFactoryInterface $factory The current form factory.
254 * @param array $options The builder options.
256 * @return FormBuilderInterface The new builder instance.
258 protected function newBuilder($name, $dataClass, FormFactoryInterface
$factory, array $options)
260 if ($this->innerType
instanceof ButtonTypeInterface
) {
261 return new ButtonBuilder($name, $options);
264 if ($this->innerType
instanceof SubmitButtonTypeInterface
) {
265 return new SubmitButtonBuilder($name, $options);
268 return new FormBuilder($name, $dataClass, new EventDispatcher(), $factory, $options);
272 * Creates a new view instance.
274 * Override this method if you want to customize the view class.
276 * @param FormView|null $parent The parent view, if available.
278 * @return FormView A new view instance.
280 protected function newView(FormView
$parent = null)
282 return new FormView($parent);