Symfony的表格不可链接吗?

时间:2015-12-05 11:36:53

标签: php symfony entity symfony-forms

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

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

有什么想法吗?

感谢所有人!

2 个答案:

答案 0 :(得分:2)

是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(),));
}

答案 1 :(得分:0)

此链接对于解决问题非常有用:

http://symfony.com/doc/current/book/forms.html#using-a-form-without-a-class

感谢大家的回答!