Dynamic form (switch object) symfony2

I am making a page for advertising. An ad can be of different types and therefore have different data. For example, a vehicle will have make and model as additional data.

Now I have one basic Doctrine of Advert , which contains the data that each advertisement requires. Various advertisements, in turn, pursue this data (doctrine2 disinatormap)

I need to fill out a form dynamically (with ajax and symfony2 forms) if the user decides to create a vehicle advertisement. I want to display car ad options. But I also need to change the entity to AdvertVehicle form.

Is it possible? I read the cookbook entry on the symfony2 homepage

"How to dynamically change forms using form events": This should be done by accessing the AJAX call to your application. In this controller, you can submit your form, but instead of processing it, just use the submitted form to render the updated fields. Then, the response from the AJAX call can be used to update the view.

I understand how to make an ajax call with my controller, and I understand how to use event forms, but how do I get an answer from a selected select-box (for example, car models)? With a new abstract type? or formbuilder?

And then, when the user really submits the form, I need to use the object of the selected type of advertisement. Can I change the object according to the choice of users in the form dynamically?

Edit Thank you. I extend AdvertType and override the buildForm() method, and before adding the elements that I need for AdvertVehicleType , I call the parent method.

Next Explanation Each ad object contains price , description , title and category . Some advertisements contain more, such as make and model . They differ discriminatormap (doctrine2)

Example:

 // -- Entity class CarAdvert extends Advert { protected $model; protected $make; } // -- Entity // -- This uses discriminator mapping class Advert { protected $title; protected $description; protected $price; protected $category; } 

if the user selects the category cars I want to use the CarAdvert object (for verification and perseverance) if the user selects the category of elements hold house. I just want to use a regular Advert object.

One serious problem still is that i cant figure out how to make an extended form using ajax. Any tips on this part? When a user selects a car as a category, I want the form to be updated (via jQuery / ajax), but how can I create a controller that retrieves only the extended part of the form and sends the html back to the response (without using a branch and rendering it to submission, is this possible?)

Decision:

See the answer below!

+6
source share
1 answer

Decision:

The solution to my problem was to create several additional functions in the controller in order to solve the problem where I want to be able to change the essence and form on the fly from the selection by the user.

 public function indexAction(Request $request) { $form = $this->getForm($request); $form->handleRequest($request); return array( 'form' => $form->createView(), 'request' => $request->request, ); } 

Where getForm retrieves the form (for example, AdvertVehicleType for vehicles or Ad type for the default ad).

The getForm method is as follows:

  private function getForm(Request $request) { $categoryTitle = 'NONE'; $categoryId = $request->request->get('advert', false)['category']; if ($categoryId) { $categoryTitle = $this->getDoctrine()->getRepository('Bundle:Category')->find($categoryId)->getTitle(); } return $this->createForm($this->getFormType($categoryTitle), $this->getEntity($categoryTitle)); 

}

here I extract the categoryID (which is selected on the form in the request) and returns the type of the form with getFormType and the object with getEntity.

 private function getEntity($categoryTitle) { $entity = new Advert(); switch ($categoryTitle) { case Category::CARS: $entity = new AdvertCar(); } return $entity; } private function getFormType($categoryTitle) { switch ($categoryTitle) { case Category::CARS: return new AdvertCarType(); default: return new AdvertType(); } } 

To update this on the fly using ajax (but it also works if the user tries to submit the form), I created another action in the controller.

This action displays the parts of the form that I want to update (when calling ajax), I do this by actually choosing what I don’t need in the form with a branch, setting the form objects to display as follows:

 {% do form.title.setRendered %} 

(this is just an example that I actually do for all form objects that I don't want to display.

Then I just call:

 {{ form_rest(form) }} 

which will retrieve the "remainder" of the form, which is different for different categories.

Now let me say that you have a state, not a city. First select a state, after which you will create cities for this state in the branch (but then you can just display the part you need, for example {{ form_row(form.towns) }} , and return this processed template as json-response and just put it in a div want with jquery.

 $html = $this->renderView('@Bundle/NewAddPage/filter_area.twig', array('form' => $form->createView())); 

and then returns the $ html variable in the response.

I hope this helps, and that the explanation is good enough, if not just a comment, and I will update it with my answer!

+2
source

Source: https://habr.com/ru/post/956847/


All Articles