Symfony 3表单集合和子集合

时间:2018-09-26 08:23:24

标签: symfony symfony-forms

我有一个表单,其字段名为“ planes”。

此字段可以是多个(集合),其中包含以下子字段: -速度 -重量 -颜色 -引擎

“ engines”子字段也是字段的集合: -转速 - 燃油类型 -重量

我想知道Symfony表单生成器会是什么样子,因为用户应该可以随意添加任意数量的平面。引擎也一样。

1 个答案:

答案 0 :(得分:1)

它看起来像这样:

具有飞机集合的班级建造者:

df_itpl <- data.frame(month = seq(min(df$month), max(df$month), .1))

df_itpl$US <- approx(x = df$month, y = df$US, xout = df_itpl$month)$y

df_itpl$lim <- approx(x = df$month, y = df$lim, xout = df_itpl$month)$y

ggplot(df_itpl, aes(x = month, 
               ymin = ifelse(lim>US, lim, NA), 
               ymax = US, 
               y = US)) +
  geom_ribbon(fill = "pink") +
  geom_line(color = "deepskyblue4")+
  geom_line(aes(y = lim))+
  theme_bw()

PlaneType类中的构建器:

public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('planes', CollectionType::class, array(
            'entry_type' => PlaneType::class,
            'allow_add'    => true,
            'allow_delete' => true, 
            'by_reference' => false,// this will call get/set of your entity
            'prototype' => true, // is needed coz there will be 2 prototypes 
            'prototype_name'=> '__planes__'//1-st prototype name

        ));
    }

EngineType类中的生成器:

public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('speed');
        $builder->add('weight');
        $builder->add('color');
        $builder->add('engines', CollectionType::class, array(
            'entry_type' => EngineType::class,
            'allow_add'    => true,
            'allow_delete' => true, 
            'by_reference' => false,// this will call get/set of your entity
            'prototype' => true, // is needed coz there will be 2 prototypes 
            'prototype_name'=> '__engines__'//2 -nd prototype

        ));
    }

此外,您还应该添加一些用于动态添加/删除字段的javascript,在添加过程中,您应该使用字段编号ID交换原型名称。