Symfony更改ChoiceType多个扩展输入名称

时间:2016-12-20 16:53:26

标签: symfony symfony-forms

位置为:

id    name        title
---   -----       ------
1     usa         United States of America
2     uk          United Kingdom
3     canada      Canada

我有一个** EntityType **表单字段,名为 location

class OrderType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('location', EntityType::class, array(
            'class'        => Location::class,
            'choice_label' => 'title',
            'choice_name'  => 'name',
            'choice_value' => 'id',
            'multiple'     => true,
            'expanded'     => true,
            'data'         => array(
                'usa', 'canada'
            ),
        ));
    }
}

我想要检查一些复选框(EntityTypemultiple=trueexpanded=true)(使用data选项)。

我将choice_name选项设置为位置名称,但name表单字段属性不会更改。

HTML

<input type="checkbox" id="order_location_usa" name="order[location][]" value="1">
<input type="checkbox" id="order_location_uk" name="order[location][]" value="2">
<input type="checkbox" id="order_location_canada" name="order[location][]" value="3">

我错过了什么?

1 个答案:

答案 0 :(得分:0)

如果你可以传入一个实体,你可以这样做:

'choice_name' => function($location){
    return $location->getName();
}

否则,&#39; name&#39;没有定义,这就是为什么它是空白的。文档在这里:https://symfony.com/doc/current/reference/forms/types/entity.html#choice-name

所以choice_name是可调用的(函数)。