Manipulate form data before persisting entity - Symfony 2.6

1.3k Views Asked by At

I have an entity which has a field called end_date which is datetime. In my symfony form I have a unbinded field which you can select an amount of days e.g 3 days, 5days, 7 days.

What i need to do is manipulate the amount of days provided by the form (which i don't want to go into the db) and do the calculations of when the end _date would be in relation to the amount of days selected and then persist the end_date.

What im having trouble with is how to do the data manipulation in between creating the form and persisting the entities to the db.

Here is my functionality for the form as it stands:

public function saveNewListing($request, $controller){
        $listing = new Listing();
        $product = new Product();
        $listing->setProduct($product);
        $product->setUser($controller->getUser());
        $form = $controller->createForm(new SellType(), $auction)->handleRequest($request);

        if($form->isValid()) {
            $em = $controller->getDoctrine()->getManager();
            $em->persist($listing);
            $em->persist($product);
            $em->flush();
            return true;
        }
        return $form;
    }

So inbetween binding the form data and the entities and checking if the form is valid or not, where should i manipulate the data? do i need to bind the new end_date to the form as well? or just bind it to the entity?

Thanks

3

There are 3 best solutions below

0
On BEST ANSWER
   if($form->isValid()) {
            $em = $controller->getDoctrine()->getManager();

            $days = $request->get("form")["days"] 
            //* calc end date here $endDate as \DateTime */
            $listing->setEndDate($endDate);

            $em->persist($listing);
            $em->persist($product);
            $em->flush();
            return true;
 }
3
On

Although @Evgeniy's answer is technically correct I want to expand it a little bit.

When you create a form in Symfony and then call the handleRequest method, the form will be populated with all of the submitted fields. If you want to manipulate your form's data you have two possible options:

  1. Do any necessary modifications in the Controller in the if($form->isValid()){} code block.
  2. Create form events and manipulate the data before/after it has been populated. (Form events, Dynamic form modifications)

Both options are equally right. What you choose depends on your use case:

  1. If you're going to be using this FormType in one particular place, you can easily go with defining your logic inside the Controller.
  2. If, however, your FormType is going to be included in other places and needs to maintain the same functionality you should go with writing form event listeners/subscribers. This will eliminate the need of writing any code inside the Controller and give you the final Entity ready to be saved into the database without the need of any further modifications (as they have already been done in the event listener you've created).
1
On

I recommend to you to do that in callback on prePersist. This is an good practice. See there more about this http://symfony.com/doc/current/book/doctrine.html#lifecycle-callbacks