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\Extension\Core\Type
;
14 use Symfony\Component\Form\AbstractType
;
15 use Symfony\Component\Form\FormBuilderInterface
;
16 use Symfony\Component\Form\FormInterface
;
17 use Symfony\Component\Form\FormView
;
18 use Symfony\Component\OptionsResolver\OptionsResolverInterface
;
21 * Encapsulates common logic of {@link FormType} and {@link ButtonType}.
23 * This type does not appear in the form's type inheritance chain and as such
24 * cannot be extended (via {@link FormTypeExtension}s) nor themed.
26 * @author Bernhard Schussek <bschussek@gmail.com>
28 abstract class BaseType
extends AbstractType
33 public function buildForm(FormBuilderInterface
$builder, array $options)
35 $builder->setDisabled($options['disabled']);
41 public function buildView(FormView
$view, FormInterface
$form, array $options)
43 $name = $form->getName();
44 $blockName = $options['block_name'] ?: $form->getName();
45 $translationDomain = $options['translation_domain'];
48 if ('' !== ($parentFullName = $view->parent
->vars
['full_name'])) {
49 $id = sprintf('%s_%s', $view->parent
->vars
['id'], $name);
50 $fullName = sprintf('%s[%s]', $parentFullName, $name);
51 $uniqueBlockPrefix = sprintf('%s_%s', $view->parent
->vars
['unique_block_prefix'], $blockName);
55 $uniqueBlockPrefix = '_'.$blockName;
58 if (!$translationDomain) {
59 $translationDomain = $view->parent
->vars
['translation_domain'];
64 $uniqueBlockPrefix = '_'.$blockName;
66 // Strip leading underscores and digits. These are allowed in
67 // form names, but not in HTML4 ID attributes.
68 // http://www.w3.org/TR/html401/struct/global.html#adef-id
69 $id = ltrim($id, '_0123456789');
72 $blockPrefixes = array();
73 for ($type = $form->getConfig()->getType(); null !== $type; $type = $type->getParent()) {
74 array_unshift($blockPrefixes, $type->getName());
76 $blockPrefixes[] = $uniqueBlockPrefix;
78 if (!$translationDomain) {
79 $translationDomain = 'messages';
82 $view->vars
= array_replace($view->vars
, array(
86 'full_name' => $fullName,
87 'disabled' => $form->isDisabled(),
88 'label' => $options['label'],
90 'attr' => $options['attr'],
91 'block_prefixes' => $blockPrefixes,
92 'unique_block_prefix' => $uniqueBlockPrefix,
93 'translation_domain' => $translationDomain,
94 // Using the block name here speeds up performance in collection
95 // forms, where each entry has the same full block name.
96 // Including the type is important too, because if rows of a
97 // collection form have different types (dynamically), they should
98 // be rendered differently.
99 // https://github.com/symfony/symfony/issues/5038
100 'cache_key' => $uniqueBlockPrefix.'_'.$form->getConfig()->getType()->getName(),
107 public function setDefaultOptions(OptionsResolverInterface
$resolver)
109 $resolver->setDefaults(array(
110 'block_name' => null,
114 'translation_domain' => null,
117 $resolver->setAllowedTypes(array(