Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
655 views
in Technique[技术] by (71.8m points)

symfony 2.1 - Customize form field rendering

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.
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Found a solution

What i have done is:

  1. Created a field type, lets call it myfieldType in myCompanymyBundleFormTypemyfieldType.php

    namespace myCompanymyBundleFormType;
    
    use SymfonyComponentFormAbstractType;
    use SymfonyComponentFormFormBuilder;
    
    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: myCompanymyBundleFormTypemyfieldType
        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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...