/** * Adds a CSRF field to the root form view. * * @param FormView $view The form view * @param FormInterface $form The form */ public function finishView(FormViewInterface $view, FormInterface $form, array $options) { if ($options['csrf_protection'] && !$view->hasParent() && $options['compound']) { $factory = $form->getConfig()->getAttribute('csrf_factory'); $data = $options['csrf_provider']->generateCsrfToken($options['intention']); $csrfForm = $factory->createNamed($options['csrf_field_name'], 'hidden', $data, array('mapped' => false)); $view->add($csrfForm->createView($view)); } }
/** * {@inheritdoc} */ public function buildView(FormViewInterface $view, FormInterface $form, array $options) { $name = $form->getName(); $blockName = $options['block_name'] ?: $form->getName(); $readOnly = $options['read_only']; $translationDomain = $options['translation_domain']; if ($view->hasParent()) { if ('' === $name) { throw new FormException('Form node with empty name can be used only as root form node.'); } $parentView = $view->getParent(); if ('' !== ($parentFullName = $parentView->getVar('full_name'))) { $id = sprintf('%s_%s', $parentView->getVar('id'), $name); $fullName = sprintf('%s[%s]', $parentFullName, $name); $fullBlockName = sprintf('%s_%s', $parentView->getVar('full_block_name'), $blockName); } else { $id = $name; $fullName = $name; $fullBlockName = '_' . $blockName; } // Complex fields are read-only if they themselves or their parents are. if (!$readOnly) { $readOnly = $parentView->getVar('read_only'); } if (!$translationDomain) { $translationDomain = $parentView->getVar('translation_domain'); } } else { $id = $name; $fullName = $name; $fullBlockName = '_' . $blockName; // Strip leading underscores and digits. These are allowed in // form names, but not in HTML4 ID attributes. // http://www.w3.org/TR/html401/struct/global.html#adef-id $id = ltrim($id, '_0123456789'); } $types = array(); for ($type = $form->getConfig()->getType(); null !== $type; $type = $type->getParent()) { array_unshift($types, $type->getName()); } if (!$translationDomain) { $translationDomain = 'messages'; } $view->addVars(array('form' => $view, 'id' => $id, 'name' => $name, 'full_name' => $fullName, 'full_block_name' => $fullBlockName, 'read_only' => $readOnly, 'errors' => $form->getErrors(), 'valid' => $form->isBound() ? $form->isValid() : true, 'value' => $form->getViewData(), 'disabled' => $form->isDisabled(), 'required' => $form->isRequired(), 'max_length' => $options['max_length'], 'pattern' => $options['pattern'], 'size' => null, 'label' => $options['label'], 'multipart' => false, 'attr' => $options['attr'], 'label_attr' => $options['label_attr'], 'compound' => $form->getConfig()->getCompound(), 'types' => $types, 'translation_domain' => $translationDomain)); }