2011-09-30 55 views
3

我正在開發一個關於symfony 2框架的博客類工具,其中一篇文章可以有很多評論。我在各自實體的帖子和評論之間建立了一對多的關係。當主頁上顯示帖子時,我需要一個評論表單,用戶可以在其中發表評論。爲了發表評論,我必須在表單中爲postID傳遞一個隱藏字段,但我很難這樣做。下面是我的全部代碼..Symfony 2將變量傳遞給表單類以動態創建表單中的隱藏字段

控制器:

<?php 

namespace Issh\MainBundle\Controller; 

use Symfony\Bundle\FrameworkBundle\Controller\Controller; 
use Symfony\Component\HttpFoundation\Response; 
use Issh\MainBundle\Form\Post\CommentForm; 
use Issh\MainBundle\Form\Post\PostForm; 
use Issh\MainBundle\Entity\IsshPost; 
use Issh\MainBundle\Entity\IsshComment; 

class HomeController extends Controller 
{ 

    public function indexAction() 
    { 
     return $this->render('IsshMainBundle:Home:index.html.php'); 
    } 

    public function postAction() 
    { 
     $em = $this->get('doctrine')->getEntityManager(); 
     $request = $this->get('request'); 

     $post = new IsshPost();   
     $form = $this->createForm(new PostForm(), $post); 

     if ('POST' == $request->getMethod()) 
     {   
      $form->bindRequest($request);   
      $post->setIsshUser($this->get('security.context')->getToken()->getUser()); 

      if ($form->isValid()) 
      { 
       $em->persist($post); 
       $em->flush(); 

       return $this->redirect($this->generateUrl('home')); 
      } 
      return $this->render('IsshMainBundle:Home:IsshPost.html.php', array(
       'form' => $form->createView())); 
     } 
     else 
     { 
      $em = $this->getDoctrine()->getEntityManager(); 
      $posts = $em->getRepository('IsshMainBundle:IsshPost')->getLatestPosts(); 
      return $this->render('IsshMainBundle:Home:IsshPost.html.php', array(
       'form' => $form->createView(), 'posts' => $posts));   
     } 

    } 

    public function commentAction($postID = null) 
    { 
     $em = $this->get('doctrine')->getEntityManager(); 
     $request = $this->get('request'); 

     $comment = new IsshComment();   
     $form = $this->createForm(new CommentForm($postID), $comment); // need to pass postID here so it can be set as hidden field 

     if ('POST' == $request->getMethod()) 
     {   
      $form->bindRequest($request);   
      $comment->setIsshUser($this->get('security.context')->getToken()->getUser()); 

      if ($form->isValid()) { 
       $em->persist($comment); 
       $em->flush(); 

       return $this->redirect($this->generateUrl('home')); 
      } 

     } 
     else 
     { 
      return $this->render('IsshMainBundle:Home:IsshComment.html.php', array(
       'form' => $form->createView())); 
     }  
    } 
} 

這裏是交形式:

<?php 

namespace Issh\MainBundle\Form\Post; 

use Symfony\Component\Form\AbstractType; 
use Symfony\Component\Form\FormBuilder; 

class PostForm extends AbstractType 
{ 
    public function buildForm(FormBuilder $builder, array $options) 
    { 
     $builder->add('text','text'); 
    } 

    public function getName() 
    { 
     return 'postForm'; 
    } 

    public function getDefaultOptions(array $options) 
    { 
     return array(
      'data_class' => 'Issh\MainBundle\Entity\IsshPost' 
     ); 
    } 
} 
?> 

評論表單(還沒有工作):

<?php 

namespace Issh\MainBundle\Form\Post; 

use Symfony\Component\Form\AbstractType; 
use Symfony\Component\Form\FormBuilder; 

class CommentForm extends AbstractType 
{ 

    public function buildForm(FormBuilder $builder, array $options) 
    { 
     $builder->add('text','text'); 
    } 

    public function getName() 
    { 
     return 'commentForm'; 
    } 

    public function getDefaultOptions(array $options) 
    { 
     return array(
      'data_class' => 'Issh\MainBundle\Entity\IsshComment' 
     ); 
    } 
} 
?> 

帖子模板:

<?php if(!empty($form)): ?> 
<form action="<?php echo $view['router']->generate('post') ?>" method="post" <?php echo $view['form']->enctype($form) ?> > 
    <?php echo $view['form']->errors($form) ?> 
    <?php echo $view['form']->row($form['text']) ?> 
    <?php echo $view['form']->rest($form) ?> 
    <input type="submit" /> 
</form> 
<?php endif; ?> 
<?php if(!empty($posts)): ?> 
    <?php foreach ($posts as $post): ?> 
    <p><?php echo $post->getText();?></p> 
    <?php 
    //embed comment controller in view 
    echo $view['actions']->render('IsshMainBundle:Home:comment',array('postID' => $post->getId())); 
    ?> 
    -------------------------------------- 
    <?php endforeach; ?> 
<?php endif; ?> 

註釋模板:

<?php if(!empty($form)): ?> 
<form action="<?php echo $view['router']->generate('comment') ?>" method="post" <?php echo $view['form']->enctype($form) ?> > 
    <?php echo $view['form']->errors($form) ?> 
    <?php echo $view['form']->row($form['text']) ?> 
    <?php echo $view['form']->rest($form) ?> 
    <input type="submit" /> 
</form> 
<?php endif; ?> 
<?php if(!empty($comments)): ?> 
    <?php foreach ($comments as $comment): ?> 
    <p><?php echo $comment->getText();?></p> 
    -------------------------------------- 
    <?php endforeach; ?> 
<?php endif; ?> 
+0

有人嗎?我非常感謝一些幫助!一直困在這 – chintan

+0

與「數據變換器」處理的正確方式,請參閱http://symfony.com/doc/current/cookbook/form/data_transformers.html – KevinS

回答

5

你可以在你的窗體類聲明構造和傳遞帖子ID的形式。

class CommentForm extends AbstractType 
{ 
    private $postId; 

    public function __construct($postId = null) 
    { 
     $this->postId = $postId; 
    } 

    public function buildForm(FormBuilder $builder, array $options) 
    { 
    ... 
} 

您現在可以在窗體的任何位置以$ this-> postId的形式訪問postID。

3

您可以簡單地添加一個選項buildForm方式類似:

$形成= $這個 - >獲取( 'form.factory') - >創建(新FormType(),陣列(「鍵'=>'var'));