Symfony 4,当我尝试提交表单时它不起作用
Symfony 4, when I try to submit a form it does not work
在此先感谢那些想帮助我的人。我正在学习 symfony 4,我正在测试如何通过从表单中获取数据来更新数据库。所以我确保它在 id 的基础上寻找 table 并用正确的表单值填充表单。但是当我提交时,表单-> isSubmitted 条件永远不会被验证。你有什么建议吗?
public function updateArticle(Request $request, $id)
{
$em = $this->getDoctrine()->getManager();
$article = $em->getRepository(Article::class)->find($id);
if (!$article) {
throw $this->createNotFoundException(
'No article found for id '.$id
);
}
$articletext = $article->getArticle();
$title = $article->getTitle();
$image = $article->getFeatureimage();
$category = $article->getCategory();
$author = $article->getAuthor();
$article->setArticle($articletext);
$article->setTitle($title);
$article->setFeatureimage($image);
$article->setCategory($category);
$article->setAuthor($author);
$form = $this->createFormBuilder($article)
->add('article', TextareaType::class)
->add('title', TextType::class)
->add('featureimage', FileType::class, array('data_class' => null,'required' => true))
->add('category', TextType::class)
->add('author', TextType::class)
->add('save', SubmitType::class, array('label' => 'Inserisci articolo'))
->getForm();
if ($form->isSubmitted()) {
$article = $form->getData();
print_r($article);
return $this->redirectToRoute('blog');
}
else
return $this->render('insert.html.twig', array(
'form' => $form->createView(),
));
}
您忘记了处理数据的行。
Look at the doc
在 if 条件之前添加这一行:
//form creation as you did but it's better to construct the form via the FormType
$form->handleRequest($request);
if ($form->isSubmitted()){
//Do some stuff
在此先感谢那些想帮助我的人。我正在学习 symfony 4,我正在测试如何通过从表单中获取数据来更新数据库。所以我确保它在 id 的基础上寻找 table 并用正确的表单值填充表单。但是当我提交时,表单-> isSubmitted 条件永远不会被验证。你有什么建议吗?
public function updateArticle(Request $request, $id)
{
$em = $this->getDoctrine()->getManager();
$article = $em->getRepository(Article::class)->find($id);
if (!$article) {
throw $this->createNotFoundException(
'No article found for id '.$id
);
}
$articletext = $article->getArticle();
$title = $article->getTitle();
$image = $article->getFeatureimage();
$category = $article->getCategory();
$author = $article->getAuthor();
$article->setArticle($articletext);
$article->setTitle($title);
$article->setFeatureimage($image);
$article->setCategory($category);
$article->setAuthor($author);
$form = $this->createFormBuilder($article)
->add('article', TextareaType::class)
->add('title', TextType::class)
->add('featureimage', FileType::class, array('data_class' => null,'required' => true))
->add('category', TextType::class)
->add('author', TextType::class)
->add('save', SubmitType::class, array('label' => 'Inserisci articolo'))
->getForm();
if ($form->isSubmitted()) {
$article = $form->getData();
print_r($article);
return $this->redirectToRoute('blog');
}
else
return $this->render('insert.html.twig', array(
'form' => $form->createView(),
));
}
您忘记了处理数据的行。 Look at the doc 在 if 条件之前添加这一行:
//form creation as you did but it's better to construct the form via the FormType
$form->handleRequest($request);
if ($form->isSubmitted()){
//Do some stuff