English 中文(简体)
1. 建立有活力的植被
原标题:Building a form with dynamic widgets

I have 3 classes : Category, Parameter and Product.

  • Category has a one-to-many relationship with Parameters.
  • Product has a one-to-many relationship with Category.
  • Parameters are attributes for a product (color, weight, size, brand, etc.).

When I select a category and create a new product I want to create a form with these parameters. How can I do this? Is it possible with symfony form framework? I hope for your help.

我试图做这样的事情:

 class ProductRepository extends EntityRepository
{

    public function getParameters()
    {
        $em = $this->getEntityManager();
        $parameters = $em->getRepository( ShopProductBundle:CatParameter )->findAll();
        $data = array();
        foreach ($parameters as $k => $value) {
            $name = $value->getId();
            $data[$name] = array("label" => $value->getName());
        }
        return $data;
    }

}

表格

class ProductType extends AbstractType
{
    public function buildForm(FormBuilder $builder, array $options)
    {
        $builder->add( name ,  text , array( label  =>  Name ));
        $data = $options[ data ];
        foreach($data as $k => $item){
            $builder->add((string)$k,  text , array( label  => $item[ label ]));
        }
    }

    public function getName()
    {
        return  shop_productbundle_categorytype ;
    }

    public function getDefaultOptions(array $options){
        return array( data_class  =>  ShopProductBundleEntityProduct );
    }
}

形成行动:

$parameters = $em->getRepository( ShopProductBundle:Product )->getParameters();
$form = $this->createForm(new ProductType(), $parameters);

最终是例外:

Expected argument of type "ShopProductBundleEntityProduct", "array" given

问题回答

I guess you re getting this exception because in your controller $parameters is not a product. You should build a new product, and add the parameters to it. Also please have a look to this article. It deals with a problem similar to yours.

一旦你以你的形式设置了数据级,你所能获取的唯一物体就是这种类型。

 data_class  =>  ShopProductBundleEntityProduct 

The way I m adding multiple parameters to a product entity is a collection of form items: How to add collection of items

或者,你只是想通过这样的参数:

$parameters = $em->getRepository( ShopProductBundle:Product )->getParameters();
$form = $this->createForm(new ProductType(), null, $parameters);

或者,如果你希望有一个实体:

$form = $this->createForm(new ProductType(), new Product(), $parameters);




相关问题
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 ...

热门标签