Symfony 3 / CollectionType字段/'entry_options'=> array('label'=>))

时间:2017-11-23 13:58:41

标签: forms symfony

我有一个嵌入formType的表单集合。

class RateType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder    ->add('rate')
                    ->add('options', CollectionType::class, array(
                'entry_type' => RatesHasOptionsType::class,
                'entry_options' => array('label' => __?????_____ ))
                )
                    ->add('save', SubmitType::class, array('label' => 'create'));
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => Rates::class,
        ));
    }
}

嵌入的表单是

$builder->add('price', MoneyType::class, array(
    'currency' => 'CHF',
));

它来自ManytoMany关系:许多Rate可以有很多选项

rates_has_options:

+-----------+---------+------+-----+---------+----------------+
| Field     | Type    | Null | Key | Default | Extra          |
+-----------+---------+------+-----+---------+----------------+
| id        | int(11) | NO   | PRI | NULL    | auto_increment |
| rate_id   | int(11) | YES  | MUL | NULL    |                |
| option_id | int(11) | YES  | MUL | NULL    |                |
| price     | double  | NO   |     | NULL    |                |
+-----------+---------+------+-----+---------+----------------+

如何将选项的标签与选项名称一起使用? 到目前为止,它给了我0,1,2,3,我认为这是选项数组的关键。

非常感谢你的帮助!

皮尔

1 个答案:

答案 0 :(得分:0)

您应该将choices数组添加到entry_options。见this example

$builder->add('favorite_cities', CollectionType::class, array(
    'entry_type'   => ChoiceType::class,
    'entry_options'  => array(
        'choices'  => array(
            'Nashville' => 'nashville',
            'Paris'     => 'paris',
            'Berlin'    => 'berlin',
            'London'    => 'london',
        ),
    ),
));

这将导致:

<select name="favorite_cities">
    <option value="nashville">Nashville</option>
    <option value="paris">Paris</option>
    <option value="berlin">Berlin</option>
    <option value="london">London</option>
</select>