Jorgeuos
Jorgeuos

Reputation: 541

Undefined method named isSubmitted? Symfony

So I've done this before, without errors, probably symfony2, and now symfony3, probably not a related issue, just sayin. How can I solve this?

Error:

Attempted to call an undefined method named "isSubmitted" of class "Symfony\Component\Form\FormView".
500 Internal Server Error - UndefinedMethodException

Code:

namespace AppBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\AbstractType;
use AppBundle\Entity\Submission;
use Symfony\Component\Form\Extension\Core\Type\TextType;


use Symfony\Component\Form\Extension\Core\Type\FormType;
use Symfony\Component\HttpFoundation\Response;


use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\FormView;
use Symfony\Component\Form\FormInterface;


class DefaultController extends Controller
{
    /**
     * @Route("/", name="homepage")
     */
    public function indexAction(Request $request)
    {

        $variables = array();

        $submission = new Submission();
        $form = $this->createFormBuilder($submission)
            ->add('name', TextType::class)
            ->add('phonenumber', TextType::class)
            ->add('email', TextType::class)
            ->add('postal', TextType::class)
            ->add('housing', TextType::class)
            ->add('project', TextType::class)
            ->add('motivation', TextType::class)
            ->add('save', SubmitType::class, array('label' => 'Create submission'))
            ->getForm()->createView();


        var_dump($form->isSubmitted());
        die();
        if ($form->isSubmitted() && $form->isValid()) {
            print_r("yeay");
            die();
            $em = $this->getDoctrine()->getManager();
            $em->persist($submission);
            $em->flush();
        }


            // $variables['form'] = $form;
        // replace this example code with whatever you need
        return $this->render('AppBundle::main.html.twig', [
            'base_dir' => realpath($this->getParameter('kernel.root_dir').'/..'),
            'form' => $form,
        ]);
    }
}

Upvotes: 0

Views: 1017

Answers (1)

Jakub Zalas
Jakub Zalas

Reputation: 36191

You've called isSubmitted() on the form view instead of the form. The form view object doesn't have this method.

Create the form like this:

$form = $this->createFormBuilder($submission)
    ->add('name', TextType::class)
    ->add('phonenumber', TextType::class)
    // ...
    ->getForm();

Create the view just before passing it to the template:

return $this->render('AppBundle::main.html.twig', [
    'form' => $form->createView(),
]);

Upvotes: 2

Related Questions