Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Symfony 4收集式变压器。如何在文本字段中优雅地显示值?_Php_Symfony_Symfony4 - Fatal编程技术网

Php Symfony 4收集式变压器。如何在文本字段中优雅地显示值?

Php Symfony 4收集式变压器。如何在文本字段中优雅地显示值?,php,symfony,symfony4,Php,Symfony,Symfony4,我和symfony 4一起工作。我有很多关系(令人惊讶的是,与问题无关)都有一个连接表。假设实体A可以有许多实体B,反之亦然。我们也假设A拥有B 我构建了一个如下所示的表单: $builder ->add('B', TextType::class, [ 'invalid_message' => 'Some invalid message', 'attr' => [ 'class' => 'tags',

我和symfony 4一起工作。我有很多关系(令人惊讶的是,与问题无关)都有一个连接表。假设实体A可以有许多实体B,反之亦然。我们也假设A拥有B

我构建了一个如下所示的表单:

$builder
    ->add('B', TextType::class, [
        'invalid_message' => 'Some invalid message',
        'attr' => [
            'class' => 'tags',
        ]
    ])
    ->add('save', SubmitType::class);

$builder->get('B')
    ->addModelTransformer($this->transformer)
;
<div class="form-group"><label for="admin_video_tags" class="required">Tags</label><input type="text" id="admin_video_tags" name="admin_video[tags]" required="required" class="tags form-control ui-autocomplete-input" value="[{&quot;name&quot;:&quot;tag_ 6&quot;,&quot;id&quot;:47},{&quot;name&quot;:&quot;tag_ 7&quot;,&quot;id&quot;:48},{&quot;name&quot;:&quot;tag_ 16&quot;,&quot;id&quot;:57}]" autocomplete="off"></div>
接下来是变压器:

public function transform($bunchOfB_s)
{
    // returns json_encode
}


public function reverseTransform($bunchOfB_s)
{
    // json_decode and transform to B entity
}
我找不到一个像样的解决方案,无法分别渲染每个B

问题在于html本身。现在看起来是这样的:

$builder
    ->add('B', TextType::class, [
        'invalid_message' => 'Some invalid message',
        'attr' => [
            'class' => 'tags',
        ]
    ])
    ->add('save', SubmitType::class);

$builder->get('B')
    ->addModelTransformer($this->transformer)
;
<div class="form-group"><label for="admin_video_tags" class="required">Tags</label><input type="text" id="admin_video_tags" name="admin_video[tags]" required="required" class="tags form-control ui-autocomplete-input" value="[{&quot;name&quot;:&quot;tag_ 6&quot;,&quot;id&quot;:47},{&quot;name&quot;:&quot;tag_ 7&quot;,&quot;id&quot;:48},{&quot;name&quot;:&quot;tag_ 16&quot;,&quot;id&quot;:57}]" autocomplete="off"></div>
标签

我想通过一些jquery插件或其他东西使用自动完成。我不需要进行ajax调用,因为这个字段总共只有大约15个值。我可以在渲染视图时简单地将它们传递给视图

我可以使用javascript清除该字段

然而,这并不是一个优雅的解决方案。这更像是一种黑客行为

理想情况下,我正在寻找更优雅的东西


老实说,我不知道是什么。我希望更了解symfony 4的人能伸出援助之手。

这并不是您想要的,但它是一个更优雅的解决方案,使用复选框或选择一个输入选择多个
B
实体(我相信您不需要带此的转换器)。这将只允许输入现有的
B

use App\Entity\B;
use Doctrine\ORM\EntityRepository;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
// ...

$builder->add('B', EntityType::class, [
    // looks for choices from this entity
    'class' => B::class,

    // function to get the B's you want as choices
    'query_builder' => function (EntityRepository $er) {
        return $er->createQueryBuilder('b')
            ->orderBy('b.name', 'ASC');
    },

    // uses the B.name property as the visible option string
    'choice_label' => 'name',

    // used to render a select box, check boxes or radios
    // both true for checkboxes
    'multiple' => true,
    'expanded' => true,
    // for select
    'expanded' => false,
]);
另一种方法(但要做更多的工作)是为每个
B
使用单个文本输入,首先使用
TextType::class
B
创建一个formType。然后在类型表单中,使用a
CollectionType::class
B
添加到
a
。此方法需要一些javascript向集合中添加行,但这将在AType表单中创建新的
B
,而不使用现有的(没有更多javascript)