我正在使用CMS选项进行symfony(4.2)项目。因此,我决定使用Sonata捆绑软件来实现WP之类的CMS。我已经安装并使用了SonataAdminBundle,SonataMediaBundle,SonataBlockBundle ...等。另外,我还创建了带有页面块基本表单字段的自定义服务,它对我来说很好用。现在,我想创建一个具有转发器字段的表单,例如克隆系统
我创建了一个名为CloneType的新表单类型,并自定义了表单模板。还在块服务文件中添加了CloneType字段,现在该表单如下图所示。
https://i.stack.imgur.com/WmYFg.png
我的输入在浏览器中检查时看起来像这样
<input type="text" id="s9fdc9db89c_settings_cxi_different_title" name="s9fdc9db89c[settings][cxi_different][title_1]" class=" form-control" value="test"> <input type="text" id="s9fdc9db89c_settings_cxi_different_title" name="s9fdc9db89c[settings][cxi_different][title_2]" class=" form-control" value="test">
src / Form / Type / CloneType.php
<?php namespace App\Form\Type; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\Form\Extension\Core\Type\FormType; use Symfony\Component\OptionsResolver\OptionsResolver; class CloneType extends AbstractType { public function getParent() { return FormType::class; } }
templates / sonataadmin / forms / clone.twig.html
{% block clone_widget %} {% spaceless %} <div class="clone-wrapper"> <div id="clonedInput1" class="clonedInput"> {{- form_widget(form) -}} {{- form_errors(form) -}} <div class="actions"> <a class="btn btn-info clone">Clone</a> <a class="btn btn-warning remove">Remove</a> </div> </div> </div> {% endspaceless %} {% block javascripts %} <script type="text/javascript"> var regex = /^(.+?)(\d+)$/i; var cloneIndex = $(".clonedInput").length; function clone(){ $(this).parents(".clonedInput").clone() .appendTo("clone-wrapper") .attr("id", "clonedInput" + cloneIndex) .find("*") .each(function() { var id = this.id || ""; var match = id.match(regex) || []; if (match.length == 3) { this.id = match[1] + (cloneIndex); } }) .on('click', 'a.clone', clone) .on('click', 'a.remove', remove); cloneIndex++; } function remove(){ $(this).parents(".clonedInput").remove(); } $("a.clone").on("click", clone); $("a.remove").on("click", remove); </script> {% endblock %} {% endblock %}
src / Application / Sonata / BlockBundle / Block / CxiDifferentBlockService.php
<?php namespace App\Application\Sonata\BlockBundle\Block; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\OptionsResolver\OptionsResolverInterface; use Symfony\Component\Form\FormEvents; use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\Form\Extension\Core\Type\FormType; use Sonata\Form\Type\ImmutableArrayType; use Sonata\Form\Validator\ErrorElement; use Sonata\AdminBundle\Form\FormMapper; use Sonata\BlockBundle\Meta\Metadata; use Sonata\BlockBundle\Block\BlockContextInterface; use Sonata\BlockBundle\Model\BlockInterface; use Sonata\BlockBundle\Block\Service\AbstractBlockService; use App\Application\Sonata\PageBundle\Entity\Block; use App\Form\Type\CloneType; use Sonata\AdminBundle\Admin\AdminInterface; class CxiDifferentBlockService extends AbstractBlockService { public function buildEditForm(FormMapper $formMapper, BlockInterface $block) { $formMapper ->add('settings', ImmutableArrayType::class, [ 'keys' => [ ['title', TextType::class, [ 'label' => 'Title (H2)', 'required' => false, ]], [$this->getCloneBuilder($formMapper), null, []], ], ]) ; } protected function getCloneBuilder(FormMapper $formMapper) { return $formMapper->create('cxi_different', CloneType::class, ['required' => false,'by_reference' => false,'allow_extra_fields'=>true]) ->add('title', TextType::class,['required' => false, 'allow_extra_fields'=>true, 'by_reference' => false]) ; } }
我想更新数据库中的克隆字段值。但是它不起作用。我在提交表单时检查了数据。奏鸣曲形式未在post数组中添加克隆的字段。帖子数组看起来像下面的实际结果
实际结果:
Array( [title] => test [cxi_different] => Array ( [title_1] => test ) )
预期结果:
Array( [title] => test [cxi_different] => Array ( [title_1] => test [title_2] => test ) )
提前感谢!
我正在使用CMS选项进行symfony(4.2)项目。因此,我决定使用Sonata捆绑软件来实现WP之类的CMS。我已经安装并使用了Currentl,并已使用SonataAdminBundle,SonataMediaBundle,...
我已经使用CollectionType实现了这一目标,并且按照以下步骤实现了这一目标