如何在symfony2中创建自定义字段类型?

时间:2022-04-22 06:45:34

I want to make a custom form field in symfony2 named daterange, which will extends the default symfony date type form field and take date range(start date and end date) into two different text-box.

我想在symfony2中创建一个名为daterange的自定义表单字段,它将扩展默认的symfony日期类型表单字段,并将日期范围(开始日期和结束日期)放入两个不同的文本框中。

3 个解决方案

#1


23  

Cause I don't like twig template engine this example only for PHP templating

因为我不喜欢twig模板引擎这个例子只用于PHP模板

What you need is to make:

你需要做的是:

  1. New TestBundle\Form\Extension\Core\Type\DateRangeType which extends Symfony\Component\Form\AbstractType

    新的TestBundle \ Form \ Extension \ Core \ Type \ DateRangeType,它扩展了Symfony \ Component \ Form \ AbstractType

    Here you should:
    a. write your own getParent, getName, buildForm methods
    b. getParent return 'field'
    c. getName return 'daterange'
    d. buildForm has $builder->add('start', ...)->add('end', ...)->setAttribute('widget', 'daterange')

    在这里你应该:a。编写自己的getParent,getName,buildForm方法b。 getParent返回'field'c。 getName返回'daterange'd。 buildForm有$ builder-> add('start',...) - > add('end',...) - > setAttribute('widget','daterange')

  2. Add it to the DI (config.yml as example)

    将其添加到DI(config.yml为例)

    services:
        form.type.daterange:
            class: TestBundle\Form\Extension\Core\Type\DateRangeType
            tags:
                -  { name: form.type, alias: daterange }
  1. Create new widget for it in TestBundle/Resources/views/Form/daterange_widget.html.php you can take date widget as example. Src/vendor/symfony/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/date_widget.html.php

    在TestBundle / Resources / views / Form / daterange_widget.html.php中为它创建新的小部件,您可以将日期小部件作为示例。 SRC /供应商/ symfony中/ src目录/ Symfony的/包/ FrameworkBundle /资源/视图/表格/ date_widget.html.php

  2. Add to config (config.yml as example)

    添加到配置(config.yml为例)

   framework:
       templating:
           form:
               resources:
                   - 'TestBundle:Form'

And for more widget customization as nefo_x said check form customization.

而对于更多的小部件定制,nefo_x表示检查表单定制。

#2


6  

In order to do that, you need to add the following lines into app/config/config.yml

为此,您需要将以下行添加到app / config / config.yml中

twig:
    form:
        resources:
            - 'YourSuperBundle:Form:fields.html.twig'

then in src/Your/SuperBundle/Resources/views/Form/fields.html.twig:

然后在src / Your / SuperBundle / Resources / views / Form / fields.html.twig中:

{% extends 'form_div_layout.html.twig' %}

{% block daterange_widget %}
     ... do the customization.
{% endblock %}

For additional reference please read form customization of Symfony 2.0 book.

如需其他参考,请阅读Symfony 2.0书籍的表格定制。

#3


2  

There is a good entry in official cookbook on creating custom field type

在官方食谱中有一个关于创建自定义字段类型的良好条目

#1


23  

Cause I don't like twig template engine this example only for PHP templating

因为我不喜欢twig模板引擎这个例子只用于PHP模板

What you need is to make:

你需要做的是:

  1. New TestBundle\Form\Extension\Core\Type\DateRangeType which extends Symfony\Component\Form\AbstractType

    新的TestBundle \ Form \ Extension \ Core \ Type \ DateRangeType,它扩展了Symfony \ Component \ Form \ AbstractType

    Here you should:
    a. write your own getParent, getName, buildForm methods
    b. getParent return 'field'
    c. getName return 'daterange'
    d. buildForm has $builder->add('start', ...)->add('end', ...)->setAttribute('widget', 'daterange')

    在这里你应该:a。编写自己的getParent,getName,buildForm方法b。 getParent返回'field'c。 getName返回'daterange'd。 buildForm有$ builder-> add('start',...) - > add('end',...) - > setAttribute('widget','daterange')

  2. Add it to the DI (config.yml as example)

    将其添加到DI(config.yml为例)

    services:
        form.type.daterange:
            class: TestBundle\Form\Extension\Core\Type\DateRangeType
            tags:
                -  { name: form.type, alias: daterange }
  1. Create new widget for it in TestBundle/Resources/views/Form/daterange_widget.html.php you can take date widget as example. Src/vendor/symfony/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/date_widget.html.php

    在TestBundle / Resources / views / Form / daterange_widget.html.php中为它创建新的小部件,您可以将日期小部件作为示例。 SRC /供应商/ symfony中/ src目录/ Symfony的/包/ FrameworkBundle /资源/视图/表格/ date_widget.html.php

  2. Add to config (config.yml as example)

    添加到配置(config.yml为例)

   framework:
       templating:
           form:
               resources:
                   - 'TestBundle:Form'

And for more widget customization as nefo_x said check form customization.

而对于更多的小部件定制,nefo_x表示检查表单定制。

#2


6  

In order to do that, you need to add the following lines into app/config/config.yml

为此,您需要将以下行添加到app / config / config.yml中

twig:
    form:
        resources:
            - 'YourSuperBundle:Form:fields.html.twig'

then in src/Your/SuperBundle/Resources/views/Form/fields.html.twig:

然后在src / Your / SuperBundle / Resources / views / Form / fields.html.twig中:

{% extends 'form_div_layout.html.twig' %}

{% block daterange_widget %}
     ... do the customization.
{% endblock %}

For additional reference please read form customization of Symfony 2.0 book.

如需其他参考,请阅读Symfony 2.0书籍的表格定制。

#3


2  

There is a good entry in official cookbook on creating custom field type

在官方食谱中有一个关于创建自定义字段类型的良好条目