【问题标题】:Symfony form - choiceType from class attributesSymfony 形式 - 来自类属性的选择类型
【发布时间】:2017-10-16 13:31:47
【问题描述】:

我有一个类Options有很多属性,嵌套在B类中

class Options {    
    private $foo;
    private $bar;    
    // getter setters ..
}

class B {
    private $baz;
    private $options; // Option class
}

有没有办法用 B.options 属性中的复选框制作 ChoiceType ?

$options = new Options();
$b = new B();
$b->setOptions($options);

$form = $this->createForm(MyType::class, $b, []);

// ...

$builder->add('options', ??,[
        // ??
    ];

这种情况的最佳做法是什么。

【问题讨论】:

    标签: symfony formbuilder


    【解决方案1】:

    你想embed a collection of forms。 Symfony 很好地链接表单中的实体。检查文档链接。

    【讨论】:

    • (它不是实体,而是其他普通类中的普通类。)并且它不会将每个属性都放在复选框列表中......
    【解决方案2】:

    只需使用ChoiceType。如果expandedmultiple 选项设置为true,则会呈现复选框。

    $builder->add('options', ChoiceType::class, [
        'expanded' => true,
        'multiple' => true,
    ]);
    

    http://symfony.com/doc/current/reference/forms/types/choice.html#expanded

    【讨论】:

      猜你喜欢
      • 2021-12-14
      • 2022-12-19
      • 1970-01-01
      • 2014-09-11
      • 2019-07-30
      • 1970-01-01
      • 2022-11-11
      • 2019-07-29
      • 1970-01-01
      相关资源
      最近更新 更多