【问题标题】:Symfony 3 / CollectionType Field / 'entry_options' => array('label' => ))Symfony 3 / CollectionType 字段 / 'entry_options' => array('label' => ))
【发布时间】:2017-11-23 13:58:41
【问题描述】:

我有一组嵌入在 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',
));

它来自多对多关系:多速率可以有多个选项

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,我认为这是选项数组的关键。

【问题讨论】:

标签: symfony symfony-forms


【解决方案1】:

您应该将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>

【讨论】:

    猜你喜欢
    • 2017-01-15
    • 2016-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-26
    • 1970-01-01
    相关资源
    最近更新 更多