是Symfony';s的表单不可链接


Are Symfony's forms not linkable?

我正在用Symfony2框架开发一个轻量级应用程序。

因此,我需要创建一个没有实体链接的表单;因为我要填写的实体不适合使用表单。

有什么想法吗?

谢谢大家!

Yes Symfony支持未链接到实体的表单,下面的片段显示了如何创建未绑定到实体的联系人表单。

public function indexAction(Request $request)
{
    $form = $this->createFormBuilder()
    ->setAction($this->generateUrl('contact_route'))
    ->setMethod('POST')
    ->add('name', 'text')
    ->add('email', 'email')
    ->add('phone', 'text')
    ->add('message', 'textarea')
    ->add('submit', 'submit', array('label' => 'SUBMIT'))
    ->getForm()
    ;
    $form->handleRequest($request);
    if ($form->isValid()) {
        $em = $this->getDoctrine()->getManager();
         $message = 'Swift_Message::newInstance()
        ->setSubject(''.$form->get('name')->getData() ."  ". $form->get('phone')->getData())
        ->setFrom($form->get('email')->getData())
        ->setTo('email@ehost.com')
        ->setBody(''.$form->get('email')->getData().' '.$form->get('message')->getData());
        $this->addFlash('notice','Thank you, we will contact you soon!');
        $this->get('mailer')->send($message);

        return $this->redirect($this->generateUrl('contact_route'));
    }
    return $this->render('BundleName:Contact:index.html.twig',array('form' => $form->createView(),));
}

此链接对于回答以下问题非常有用:

http://symfony.com/doc/current/book/forms.html#using-不带a类的a型

感谢大家的回答!