简体   繁体   English

Symfony2自定义表单字段

[英]Symfony2 custom form field

I am very new to Symfony, so question might seem a little simple, but I need a help. 我是Symfony的新手,所以问题似乎有点简单,但我需要帮助。

I have generated new bundle. 我已经生成了新的包。

I have added a new route in Me\\MyBundle\\Resources\\config\\routing.yml : 我在Me\\MyBundle\\Resources\\config\\routing.yml添加了一个新路由:

my_homepage:
    pattern:  /
    defaults: { _controller: MeMyBundle:Default:index }

Bundle controller looks in simple like this: 捆绑控制器看起来像这样简单:

namespace Me\MyBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class DefaultController extends Controller
{
    public function indexAction()
    {
        $form = $this->createFormBuilder()
            ->getForm()
        ;
        return $this->render('MeMyBundle::index.html.twig', array(
            'form'        => $form->createView(),
            'param1'      => 'some_string_1',
            'param2'      => 'another string',
        ));
   }
}

In the twig template I can read and process proper params , as I want. 在枝条模板中,我可以按照自己的意愿阅读和处理适当的params

Whole action happens in the generated form, where there are AJAX requests routed to another controller. 整个动作发生在生成的表单中,其中有AJAX请求路由到另一个控制器。

What I want to achieve is create a new custom form field, which could be reued in same form multiple times, with different params . 我想要实现的是创建一个新的自定义表单字段,可以使用不同的params以相同的形式多次返回。

For example, I would like my indexAction() would have looked like this: 例如,我希望我的indexAction()看起来像这样:

    public function indexAction()
    {
        $paramsArr_1 = array(
            'param1'      => 'some_string_1',
            'param2'      => 'another string',
        );
        $paramsArr_2 = array(
            'param1'      => 'some_string_2',
            'param2'      => 'another fine string',
        );
        $form = $this->createFormBuilder()
            ->add(myCustomField, $paramsArr_1)
            ->add(myCustomField_2, $paramsArr_2)
            ->getForm()
        ;
        return $this->render('MeMyBundle::index.html.twig', array(
            'form'        => $form->createView()
        ));
   }

Yes, I did see this article , but it did not help me much. 是的,我确实看到了这篇文章 ,但它对我帮助不大。 I could not get it working. 我无法让它发挥作用。

Any help is much appreciated. 任何帮助深表感谢。

From what I know form fields extends the base form class, so your 'myCustomField' can be another form actually. 据我所知,表单字段扩展了基本表单类,因此您的“myCustomField”实际上可以是另一种形式。

Check this: http://symfony.com/doc/current/reference/forms/types/form.html 请查看: http//symfony.com/doc/current/reference/forms/types/form.html

As you know each Form object has attached an object to it, so instead of your arrays you could create a new object with those values set on it, and then add that form how many times your want with objects containing different data. 如您所知,每个Form对象都附加了一个对象,因此您可以创建一个新对象而不是您的数组,并在其上设置这些值,然后将该表单添加到包含不同数据的对象所需的次数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM