Lavin Bassam
Lavin Bassam

Reputation: 41

symfony 2.8 using if ($form->isSubmitted() && $form->isValid())

hello im trying to complete this tutorial from this link https://www.tutorialspoint.com/symfony/symfony_complete_working_example.htm

ive done until Step 15: Collect Book Information and Store It

when i try to input from the newAction form i got the error message

Warning: count(): Parameter must be an array or an object that implements Countable

oh i forgot mention im using symfony 2.8.3

here is my code

<?php
// scr/AppBundle/Controller/BooksController.php
namespace AppBundle\Controller;

use AppBundle\Entity\Book;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;

class BooksController extends Controller {
    /**
     * @Route("/books/author")
     */
    public function authorAction() 
    {
        return $this->render('books/author.html.twig');
    }

    /**
     * @Route("/books/display", name = "app_book_display")
     */
    public function displayAction()
    {
        $bk = $this->getDoctrine()
        ->getRepository('AppBundle:Book')
        ->findAll();

        return $this->render('books/display.html.twig', array('data' => $bk));
    }

    /**
     * @Route("/books/new", name = "app_book_new")
     */
    public function newAction(Request $request)
    {
        $book = new Book();
           $form = $this->createFormBuilder($book)
              ->add('name', TextType::class)
              ->add('author', TextType::class)
              ->add('price', TextType::class)
              ->add('save', SubmitType::class, array('label' => 'Submit'))
              ->getForm();

           $form->handleRequest($request);

           if ($form->isSubmitted() && $form->isValid()) {
               $book = $form->getData();
               $doct = $this->getDoctrine()->getManager();

               // tells Doctrine you want to save the Product
               $doct->persist($book);

               // executes the queries (i.e. the INSERT query)
               $doct->flush();

               return $this->redirectToRoute('app_book_display');
           } else {
            return $this->render('books/new.html.twig', array('form' => $form->createView(),
        ));
        }
    }

    /**
     * @Route("/books/update/{id}", name = "app_book_update")
     */
    public function updateAction($id, Request $request)
    {
        $doct = $this->getDoctrine()->getManager();
            $bk = $doct->getRepository('AppBundle:Book')->find($id);

            if (!$bk) {
                throw $this->createNotFoundException(
                    'No book found for id '.$id
                );
            }
            $form = $this->createFormBuilder($bk)
                ->add('name', TextType::class)
                ->add('author', TextType::class)
                ->add('price', TextType::class)
                ->add('save', SubmitType::class, array('label' => 'Submit'))
                ->getForm();

            $form->handleRequest($request);

            if ($form->isSubmitted() && $form->isValid()) {
                $book = $form->getData();
                $doct = $this->getDoctrine()->getManager();

                // tells Doctrine you want to save the Product
                $doct->persist($book);

                // executes the queries (i.e. the INSERT query)
                $doct->flush();

                return $this->redirectToRoute('app_book_display');
            } else {
                return $this->render('books/new.html.twig', array(
                    'form' => $form->createView(),
                ));
            }
    }

    /**
     * @Route("/books/delete/{id}", name = "app_book_delete")
     */
    public function deleteAction($id)
    {
        $doct = $this->getDoctrine()->getManager();
        $bk = $doct->getRepository('AppBundle:Book')->find($id);

        if (!$bk) {
            throw $this->createNotFoundException('No Book found for id '.$id);
        }
        $doct->remove($bk);
        $doct->flush();
        return $this->redirectToRoute('app_book_display');
    }
}

i expect the output will be the input from form newAction will store the data and redirect me to display and show it the data from previous input

Upvotes: 1

Views: 662

Answers (1)

Lavin Bassam
Lavin Bassam

Reputation: 41

okay i solved this issue just create project using Symfony 2.8.39 they has fixed this issue https://symfony.com/blog/symfony-2-8-39-released

Upvotes: 1

Related Questions