Symfony2 Forms Create custom fields that are used more than once

Source: Internet
Author: User
I added the following routes in ME\MYBUNDLE\RESOURCES\CONFIG\ROUTING.YML:

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

Bundle Controller

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

I want to create a custom form field that can be reused in the same form table dropdowns, with the following code

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()        ));   }

Reference the official documentation, but still not, who knows?

Reply content:

I added the following routes in ME\MYBUNDLE\RESOURCES\CONFIG\ROUTING.YML:

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

Bundle Controller

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

I want to create a custom form field that can be reused in the same form table dropdowns, with the following code

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()        ));   }

Reference the official documentation, but still not, who knows?

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.