Symfony3 Information



  • I'm making a mistake. Expected argument of type "string," AppBundle\Form\BookType given

    500 Internal Server Error - UnexpectedTypeException

    public function newAction(Request $request)
    {
        $book = new Book();
        $form = $this->createForm(new BookType(), $book);
        $form->handleRequest($request);
    
    if ($form->isSubmitted() && $form->isValid()) {
        $em = $this->getDoctrine()->getManager();
        $em->persist($book);
        $em->flush();
    
        return $this->redirectToRoute('book_show', array('id' => $book->getId()));
    }
    
    return $this->render('book/new.html.twig', array(
        'book' => $book,
        'form' => $form->createView(),
    ));
    

    }

    class BookType extends AbstractType
    {
    /**

    • @param FormBuilderInterface $builder
    • @param array $options
      */
      public function buildForm(FormBuilderInterface $builder, array $options)
      {
      $builder
      ->add('name', 'text')
      ;
      }

    /**

    • @param OptionsResolver $resolver
      */
      public function configureOptions(OptionsResolver $resolver)
      {
      $resolver->setDefaults(array(
      'data_class' => Book::class,
      ));
      }

    /**

    • @return string
      */
      public function getName()
      {
      return 'book_form';
      }


  • Symfony3 cannot now be transmitted FormType As an object, it is always necessary to transfer the full name of the class. In your case, you have to give it to me.

    $form = $this->createForm(BookType::class, $book);
    

Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2