Symfony2 - Using Form Builder Without Any Entity Attached Symfony2 - Using Form Builder Without Any Entity Attached symfony symfony

Symfony2 - Using Form Builder Without Any Entity Attached


Don't use a formType and you don't need to attach an entity in order to use the Form Builder. Simply use an array instead. You probably overlooked this small section in the Symfony documentation: http://symfony.com/doc/current/form/without_class.html

<?php// inside your controller ...$data = array();$form = $this->createFormBuilder($data)    ->add('query', 'text')    ->add('category', 'choice',        array('choices' => array(            'judges'   => 'Judges',            'interpreters' => 'Interpreters',            'attorneys'   => 'Attorneys',        )))    ->getForm();if ($request->isMethod('POST')) {    $form->handleRequest($request);    // $data is a simply array with your form fields     // like "query" and "category" as defined above.    $data = $form->getData();}


You can also use createNamedBuilder method for creating form

$form = $this->get('form.factory')->createNamedBuilder('form', 'form')            ->setMethod('POST')            ->setAction($this->generateUrl('upload'))            ->add('attachment', 'file')            ->add('save', 'submit', ['label' => 'Upload'])            ->getForm();