symfony 2表单类型集合,从模型更改每个新属性的标签

时间:2012-01-11 23:38:59

标签: forms symfony

我有4个实体ProductProductFeaturesGoodsGoodsFeaturesValue以及它们之间的关系。 我为Features添加了一些Product,然后我创建了包含静态字段的表单商品+来自Features的一些新Product用于此GoodsGoods中保存的每个GoodsFeaturesValue的值。

如何以“symfony方式”构建此表单?

已更新

我使用其他Features的集合,这工作正常,但我如何为每个值设置ProductFeatures关系的标签?渲染模板时,我可以这样做,但这很糟糕:)?

//GoodsFormType class
public function buildForm(FormBuilder $builder, array $options)
{
    $builder
            ->add('name')
            //other property...
            ->add('values', 'collection', array(
                'required' => true,
                'type' => new GoodsFeaturesValueFormType(),
                'allow_add' => false,
                'allow_delete' => false,
                'by_reference' => false,
            ))
    ;
}

//GoodsFeaturesValueFormType
    public function buildForm(FormBuilder $builder, array $options)
{
    $builder
            ->add('value', 'text')
    ;
}
  //controller
  public function saveAction($id)
{
    $em = $this->getDoctrine()->getEntityManager();
    $product = $em->getRepository('ShopCoreBundle:Product')->find($id);

    if (!$product)
        throw $this->createNotFoundException(sprintf('Product with id %s not found', $id));

    $features = $em->getRepository('ShopCoreBundle:ProductFeatures')->findByProduct($id);
    $goods = new Goods();
    $goods->setProduct($product);

    foreach ($features as $feature) {
        $entity = new GoodsFeaturesValue();
        $entity->setFeatures($feature);
        $entity->setGoods($goods);
        $entity->setProduct($product);
        $goods->addGoodsFeaturesValue($entity);
    }

    $request = $this->getRequest();

    $form = $this->createForm(new GoodsFormType(), $goods);
    $form->bindRequest($request);

    if ($form->isValid()) {
        $em->persist($goods);
        $em->flush();
        return $this->redirect($this->generateUrl('core_product_index'));
    }



    return array(
        'form' => $form->createView(),
        'goods' => $goods,
        'product' => $product,
        'features' => $features,
    );
}

2 个答案:

答案 0 :(得分:2)

这正是我想要的动态属性。 您可以使用FormEventEventSubscriber将其作为动态生成表单。 http://symfony.com/doc/master/cookbook/form/dynamic_form_generation.html

因此,在GoodsFeaturesValueFormType班级,您创建新的EventSubscriberpreSetData,请使用数据设置标签。

更新:Symfony默认ResizeFormListener没有将值传递给它,因此,这将是一个错误。要支持此功能,请修改ResizeFormListener(使用哪个集合),如下所示

[before]
    91         // Then add all rows again in the correct order                                                            
    92         foreach ($data as $name => $value) {                                                                       
    93             $form->add($this->factory->createNamed($this->type, $name, null, array_replace(array(                  
    94                 'property_path' => '['.$name.']',                                                                  
    95             ), $this->options)));
    96         }   

[modified]
    91         // Then add all rows again in the correct order                                                            
    92         foreach ($data as $name => $value) {                                                                       
    93             $form->add($this->factory->createNamed($this->type, $name, $value, array_replace(array(                  
    94                 'property_path' => '['.$name.']',                                                                  
    95             ), $this->options)));
    96         }   

答案 1 :(得分:0)