Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 - Using Form Builder Without Any Entity Attached

I have been using the form builder with Symfony2, and find it quite nice. I find myself wanting to create a search page with a series of boxes at the top to filter search results. I have three different entities as of now (judges, interpreters, attorneys). I would like the users to be able to enter partial or complete names, and have it search all of the entities. I can handle the actual searching part, but the form builder generation is what is giving me trouble.

What I am trying to do is create a form not attached to any particular entity. All the tutorials and documentation I've read on the Symfony site acts like it should be attached to an entity by default. I am wondering if I should just attach it to any entity and just set each text field to mapped = false, if this is an instance where I should just hard code the form myself, or if there is some way to do this within form builder.

like image 474
Josh Wa Avatar asked Jun 02 '13 13:06

Josh Wa


People also ask

What is Symfony and how does it work?

Symfony is very powerful when it comes to abstracting your data and this makes the form building very easy. If, however, you need some ‘on the fly’ processing of a form that does not save anything, you can build the form right into the View and then catch the submitted values in the controller.

Is it possible to use Form Builder without form type?

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

How do I validate a form in Symfony?

If the form is not mapped to an object, every object in your array of submitted data is validated using the Symfony \Component \Validator \Constraints \Valid constraint, unless you disable validation.

What is the use of form2action method?

The form2Action () method is responsible for showing and processing the form to create new Article enities. First, it instantiates a new empty object of that class. Then, it creates a new form for it using the form type we defined earlier.


2 Answers

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(); } 
like image 170
lifo Avatar answered Sep 23 '22 05:09

lifo


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(); 
like image 34
Farid Movsumov Avatar answered Sep 23 '22 05:09

Farid Movsumov