所以我以前做过这个,没有错误,可能是symfony2,现在symfony3,可能不是一个相关的问题,只是说。我该如何解决这个问题?
错误:
Attempted to call an undefined method named "isSubmitted" of class "Symfony\Component\Form\FormView".
500 Internal Server Error - UndefinedMethodException
代码:
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,
]);
}
}
答案 0 :(得分:2)
您在表单视图而不是表单上调用了isSubmitted()
。 form view对象没有此方法。
创建如下表单:
$form = $this->createFormBuilder($submission)
->add('name', TextType::class)
->add('phonenumber', TextType::class)
// ...
->getForm();
在将视图传递给模板之前创建视图:
return $this->render('AppBundle::main.html.twig', [
'form' => $form->createView(),
]);