Symfony2自定义表单字段 [英] Symfony2 custom form field

查看:136
本文介绍了Symfony2自定义表单字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Symfony非常陌生,所以问题看起来有点简单,但我需要帮助。



我已经生成了新的捆绑包。 b
$ b

我在 Me \MyBundle \Resources\config\routing.yml 中添加了一条新路线:

  my_homepage:
pattern:/
默认值:{_controller:MeMyBundle:Default:index}

Bundle控制器看起来像这样简单:

  namespace Me\MyBundle\Controller; 

使用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',
));






$ b

在树枝模板中,我可以读取和处理正确的 params ,如我所愿。



整个操作发生在生成的表单中,其中有AJAX请求被路由到另一个控制器。

我想实现的是创建一个新的自定义表单域,它可以多次重复使用相同的形式,使用不同的 params 例如,我想我的 indexAction()应该看起来像这样:code>。

  public function indexAction()
{
$ paramsArr_1 = array(
'param1' =>'some_string_1',
'param2'=>'另一个字符串',
);
$ paramsArr_2 = array(
'param1'=>'some_string_2',
'param2'=>'另一个细字符串',
);
$ form = $ this-> createFormBuilder()
- > add(myCustomField,$ paramsArr_1)
- > add(myCustomField_2,$ paramsArr_2)
- > getForm()
;
return $ this-> render('MeMyBundle :: index.html.twig',array(
'form'=> $ form-> createView()
));
}

是的,我确实看到了这篇文章,但它对我没有多大帮助。我无法得到它的工作。



任何帮助非常感谢。

解决方案

从我所知的表单字段扩展了基本的表单类,所以你的'myCustomField'可以是另外一个表单。

://symfony.com/doc/current/reference/forms/types/form.htmlrel =nofollow> http://symfony.com/doc/current/reference/forms/types/form.html



正如你所知,每个Form对象都附加了一个对象,所以你可以创建一个新对象,并设置这些对象,然后使用包含不同数据的对象添加该表单所需的次数。


I am very new to Symfony, so question might seem a little simple, but I need a help.

I have generated new bundle.

I have added a new route in 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.

Whole action happens in the generated form, where there are AJAX requests routed to another controller.

What I want to achieve is create a new custom form field, which could be reued in same form multiple times, with different params.

For example, I would like my indexAction() would have looked like this:

    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.

Check this: 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.

这篇关于Symfony2自定义表单字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆