自定义表单域呈现 [英] Customize form field rendering

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

问题描述

我想从奏鸣曲管理软件包中自定义编辑页面中表单字段的呈现包含一个使用字段文本内容的小程序。



我知道我必须编辑 configureFormFields 在admin类中的函数,但我需要知道3件事:


  • 提供字段表单模板的语法是什么

  • 放置模板文件(哪个目录)的位置

  • 模板的外观。

  • 找到解决方案



    我所做的是:


    1. 创建一个字段类型,可以在myCompany中将其称为myfieldType \myBundle\Form\Type\\ \\ myfieldType.php

        namespace myCompany \myBundle\Form\Type; 

      使用Symfony \Component\Form\AbstractType;
      使用Symfony \Component\Form\FormBuilder;

      class myfieldType extends AbstractType
      {

      public function getParent()
      {
      return'text';
      }

      public function getName()
      {
      return'myfield';


      $ / code $
      注册类型在app / config / services.yml

        myCompany.myBundle.form.type.myfield:
      class:myCompany\myBundle\Form \Type\myfieldType
      tags:
      - {name:form.type,别名:myfield}


    2. 在我的myentityAdmin类中,

        protected function configureFormFields(FormMapper $ formMapper)
      {
      $ formMapper
      - > add('myfieldname','myfield')
      ...
      }
      pre>

        public function getFormTheme(){
      返回数组('myCompanymyBundle:Admin:myfield_edit.html.twig');
      }

      和模板:

        {#src / mycompany / myBundle / Resources / views / Form / myfield_edit.html.twig#} 
      {%block myfield_widget%}
      {%spaceless %}
      {{block('textarea_widget')}}
      {%endspaceless%}
      {%endblock%}


      现在我可以通过树枝变量value访问表单字段值!

      这么容易......当你得到它。


      I would like to customize the rendering of a form field in the edit page from sonata admin bundle to include an applet that uses the text content of a field.

      I know that I have to edit the configureFormFields function in the admin class, but I need to know 3 things:

      • What is the syntax to provide a field form template
      • Where to put the template file ( which directory )
      • What the template have to looks like.

      解决方案

      Found a solution

      What i have done is:

      1. Created a field type, lets call it myfieldType in myCompany\myBundle\Form\Type\myfieldType.php

        namespace myCompany\myBundle\Form\Type;
        
        use Symfony\Component\Form\AbstractType;
        use Symfony\Component\Form\FormBuilder;
        
        class myfieldType extends AbstractType
        {
        
            public function getParent()
            {
                return 'text';
            }
        
            public function getName()
            {
                return 'myfield';
            }
        }
        

      2. Registered the Type in app/config/services.yml

        myCompany.myBundle.form.type.myfield:
            class: myCompany\myBundle\Form\Type\myfieldType
            tags:
                - { name: form.type, alias: myfield }
        

      3. In my myentityAdmin class,

         protected function configureFormFields(FormMapper $formMapper)
         {
             $formMapper
             ->add('myfieldname', 'myfield')
             ...
         }
        

        and

        public function getFormTheme() {
            return array('myCompanymyBundle:Admin:myfield_edit.html.twig');
        }
        

        and the template :

        {# src/mycompany/myBundle/Resources/views/Form/myfield_edit.html.twig #}
        {% block myfield_widget %}
            {% spaceless %}
                {{ block('textarea_widget') }}
            {% endspaceless %}
        {% endblock %}
        

      And now i can access the form field value by the twig variable "value" !

      So easy... when you got it.

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

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