public function newDeliveryAction(Request $request)
    {
        if($request->isXmlHttpRequest()){
            $em = $this->container->get('doctrine')->getManager();
            $user = $this->container->get('security.token_storage')->getToken()->getUser();

            /** @var Address $address */
            $country = $em->getRepository('CoreBundle:Country')->find('es');
            $address = new Address();
            $address->setForBilling(false);
            $address->setCountry($country);
            $address->setActor($user);
            $form = $this->createForm('EcommerceBundle\Form\AddressType', $address, array('token_storage' => $this->container->get('security.token_storage')));

            if ('POST' === $request->getMethod()) {
                $form->handleRequest($request);

                if ($form->isValid()) {
                    $em->persist($address);
                    $em->flush();
                    $url = $this->container->get('router')->generate('core_actor_profile').'?delivery=1';
                    $this->container->get('session')->getFlashBag()->add('success', 'account.address.added');
                    return new JsonResponse(array('status' => 'success', 'url' => $url));
                }else{
                    $template = $this->container->get('twig')->render("EcommerceBundle:Profile:Delivery/new.html.twig", array(
                        'delivery_form' => $form->createView(),
                        'address' => $address
                    ));
                    return new JsonResponse(array('status' => 'error', 'answer' => $template));
                }
            }
        }else {
            throw new \Exception('Only by ajax');
        }
    }
public function newDeliveryAction(Request $request)
    {
        if($request->isXmlHttpRequest()){
            $em = $this->container->get('doctrine')->getManager();
            $user = $this->container->get('security.token_storage')->getToken()->getUser();

            /** @var Address $address */
            $country = $em->getRepository('CoreBundle:Country')->find('es');
            $address = new Address();
            $address->setForBilling(false);
            $address->setCountry($country);
            $address->setActor($user);
            $form = $this->createForm('EcommerceBundle\Form\AddressType', $address, array('token_storage' => $this->container->get('security.token_storage')));

            if ('POST' === $request->getMethod()) {
                $form->handleRequest($request);

                if ($form->isValid()) {
                    $em->persist($address);
                    $em->flush();
                    $url = $this->container->get('router')->generate('core_actor_profile').'?delivery=1';
                    $this->container->get('session')->getFlashBag()->add('success', 'account.address.added');
                    return new JsonResponse(array('status' => 'success', 'url' => $url));
                }else{
                    $template = $this->container->get('twig')->render("EcommerceBundle:Profile:Delivery/new.html.twig", array(
                        'delivery_form' => $form->createView(),
                        'address' => $address
                    ));
                    return new JsonResponse(array('status' => 'error', 'answer' => $template));
                }
            }
        }else {
            throw new \Exception('Only by ajax');
        }
    }
public function newDeliveryAction ( Request $ request ) { if ( $ request -> isXmlHttpRequest ( ) ) { $ em = $ this -> container -> get ( 'doctrine' ) -> getManager ( ) ; $ user = $ this -> container -> get ( 'security.token_storage' ) -> getToken ( ) -> getUser ( ) ; /** @var Address $address */ $ country = $ em -> getRepository ( 'CoreBundle:Country' ) -> find ( 'es' ) ; $ address = new Address ( ) ; $ address -> setForBilling ( false ) ; $ address -> setCountry ( $ country ) ; $ address -> setActor ( $ user ) ; $ form = $ this -> createForm ( 'EcommerceBundle\Form\AddressType' , $ address , array ( 'token_storage' => $ this -> container -> get ( 'security.token_storage' ) ) ) ; if ( 'POST' === $ request -> getMethod ( ) ) { $ form -> handleRequest ( $ request ) ; if ( $ form -> isValid ( ) ) { $ em -> persist ( $ address ) ; $ em -> flush ( ) ; $ url = $ this -> container -> get ( 'router' ) -> generate ( 'core_actor_profile' ) . '?delivery=1' ; $ this -> container -> get ( 'session' ) -> getFlashBag ( ) -> add ( 'success' , 'account.address.added' ) ; return new JsonResponse ( array ( 'status' => 'success' , 'url' => $ url ) ) ; } else { $ template = $ this -> container -> get ( 'twig' ) -> render ( "EcommerceBundle:Profile:Delivery/new.html.twig" , array ( 'delivery_form' => $ form -> createView ( ) , 'address' => $ address ) ) ; return new JsonResponse ( array ( 'status' => 'error' , 'answer' => $ template ) ) ; } } } else { throw new \ Exception ( 'Only by ajax' ) ; } }
Add delivery address

@param Request $request
@return Response

@Route("/profile/delivery/new")
@Method({"GET","POST"})
Add delivery address