【发布时间】:2018-09-02 11:36:09
【问题描述】:
我是 Symfony 3 的新手,我正在尝试构建一个页面,其中我有两个不同实体的两个表单。一个用于添加愿望,一个用于添加评论。 当我提交一个表单时,第二个会给出错误“此表单不应包含额外字段”。
我试过 $form->get('submit')->isClicked() 但没用。
我也尝试了allow_extra_fields => true,然后我收到了错误,尽管我提交了评论,但它试图插入一个愿望。此外,在尝试进行查询之前,我使用 form->isValid() 来检查表单是否有效。
它怎么可能同时提交两个表单?
这是我的控制器:
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Shaker\JRQBundle\Entity\Wish;
use Shaker\JRQBundle\Entity\User;
use Shaker\JRQBundle\Entity\Wishcom;
use Shaker\JRQBundle\Entity\Wishsol;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\FormType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
class HomeController extends Controller
{
public function wishAction (Request $request, $id) {
$wish=$this->getWish($id);
$arraywishcom=$this->getWishComs($wish);
$arraywishsol=$this->getWishSols($wish);
$wishcom = new Wishcom();
$wishsol = new Wishsol();
$formcom=$this->buildFormCom($wishcom);
$formsol=$this->buildFormSol($wishsol);
// store a comment or a solution
if ($request->isMethod('POST')) {
$formcom->handleRequest($request);
if ($formcom->isValid()) {
$em=$this->getDoctrine()->getManager();
$security = $this->container->get('security.token_storage');
$token=$security->getToken();
$user=$token->getUser();
$wishcom->setUser($user);
$wishcom->setWish($wish);
$em->persist($wishcom);
$em->flush();
$request->getSession()->getFlashBag()->add('notice', "Commentaire bien enregistré.");
}
$formsol->handleRequest($request);
if ($formsol->isValid()) {
$em=$this->getDoctrine()->getManager();
$security = $this->container->get('security.token_storage');
$token=$security->getToken();
$user=$token->getUser();
$wishsol->setUser($user);
$wishsol->setWish($wish);
$em->persist($wishsol);
$em->flush();
$request->getSession()->getFlashBag()->add('notice', "Solution bien enregistrée.");
}
}
return $this->render('ShakerJRQBundle:Home:wish.html.twig', array(
'wish' => $wish,
'formcom' => $formcom->createView(),
'formsol' => $formsol->createView(),
'wishcom' => $arraywishcom,
'wishsol' => $arraywishsol));
}
private function buildFormCom (Wishcom $wishcom) {
//build the form for discussing the topic
$formbuilder = $this->get('form.factory')->CreateBuilder(FormType::class, $wishcom, array('allow_extra_fields' => true));
$formbuilder
->add('Type', ChoiceType::class, array(
'choices' => array(
'Argument' => 'Argument',
'Contre-argument' => 'Contre-argument',
'Commentaire' => 'Commentaire')))
->add('Comment', TextareaType::class)
->add('Commenter', SubmitType::class);
$formcom=$formbuilder->getForm();
return $formcom;
}
private function buildFormSol (Wishsol $wishsol) {
//build the form for adding a solution
$formbuilder = $this->get('form.factory')->CreateBuilder(FormType::class, $wishsol, array('allow_extra_fields' => true));
$formbuilder
->add('Solution', TextareaType::class)
->add('Proposer une solution', SubmitType::class);
$formsol=$formbuilder->getForm();
return $formsol;
}
}
【问题讨论】: