English 中文(简体)
缩略语
原标题:symfony 2 form type collection, change label for each new property from model

I have 4 entities as Product, ProductFeatures, Goods, GoodsFeaturesValue and relations between them. I add some Features for Product and then I whant create form with static fields Goods + some new Features from Product for this Goods. Values for each Goods saved in GoodsFeaturesValue.

如何以“匿名方式”建立这种形式?

附文

I use Collection for other Features and this work mini, but how i can setbell from ProductFeatures relation for each Value? 在使寺庙化时,我能够这样做,但这不好:

//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,
    );
}
问题回答

This is exactly what i wanted for Dynamic Attribute. You can use FormEvent and EventSubscriber to do this as Dynamic Generate Form. http://symfony.com/doc/master/cookbook/form/dynamic_form_generation.html

So, at GoodsFeaturesValueFormType class, you create new EventSubscriber and with preSetData, set the label with data.

www.un.org/Depts/DGACM/index_spanish.htm 辛辛醇/水面法不适用,因此,这将是一个错误。 为了支持这项工作,修改了<代码>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         }   




相关问题
C# Form Problem: new form losing control and randomly hiding

I m encountering strange behavior with forms on a c# 3.5 app. On a button click, my form1 hides itself, creates a new form2, and shows form2. Form1 also contains the event method triggered when ...

TCPlistener.BeginAcceptSocket - async question

Some time ago I have payed to a programmer for doing multithread server. In the meantime I have learned C# a bit and now I think I can see the slowndown problem - I was told by that guy that nothing ...

RoR: before_save on nested object in form?

I have a form with a nested object (customer < order), and it works except that it keeps creating a new customer record. I d like to have it check to see if an existing customer is already ...

Receive POST from External Form

I have a form on another website (using a different backend) that I want to be able to POST to my Rails application (on a different domain). How do I generate a valid authenticity token for the ...

Getting posted values in MVC PartialView

I ve created a PartialView which I render with Html.RenderPartial, passing the name of the view and the strongly-typed data item to bind to (below): <% Html.RenderPartial("...

Recaptcha - Form Customization

Does anyone know if recaptcha can be fully customize without the default frame. I need the recaptcha image to only be a certain width as well as the input field. Has anyone done this before with ...

Better way to retain form data on manual back redirect?

I have a form that, on submit, requires the customer to look over the data, and then confirm the changes before saving the data. However, I m using a really rough way of retaining data if the user ...

热门标签