daily pastebin goal
39%
SHARE
TWEET

Untitled

a guest Dec 13th, 2018 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. CONTROLLER:
  2.  
  3. /**
  4.  * @Route("product")
  5.  * Class ProductController
  6.  * @package appBundle\Controller
  7.  */
  8. class ProductController extends Controller
  9. {
  10.     /**
  11.      * @Route("/", name="product_add")
  12.      * @param Request $request
  13.      * @return \Symfony\Component\HttpFoundation\Response
  14.      */
  15.     public function createProductAction(Request $request)
  16.     {
  17.         $product=new Product();
  18.         $form=$this->createForm(ProductType::class,$product);
  19.         $form->handleRequest($request);
  20.         var_dump($form);exit;
  21.  
  22.         if ($form->isSubmitted() and $form->isValid()){
  23.             $entityManager=$this->getDoctrine()->getManager();
  24.             $entityManager->persist($product);
  25.             $entityManager->flush();
  26.  
  27.             return $this->render('product/addProduct.html.twig',
  28.                 ['form'=>$form->createView(),'product'=>$product]);
  29.         }
  30.         return $this->redirectToRoute('homepage');
  31.     }
  32. }
  33.  
  34. FORM:
  35.  
  36. class ProductType extends AbstractType
  37. {
  38.     public function buildForm(FormBuilderInterface $builder, array $options)
  39.     {
  40.         $builder
  41.             ->add('name')
  42.             ->add('price')
  43.             ->add('isInStock')
  44.             ->add('category_id');
  45.     }
  46.  
  47.     public function configureOptions(OptionsResolver $resolver)
  48.     {
  49.         $resolver->setDefault('data_class', Product::class);
  50.     }
  51. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top