小编chi*_*tan的帖子

Symfony 2将变量传递给表单类,以在表单中动态创建隐藏字段

我正在研究一种关于symfony 2框架的博客工具,其中一篇帖子可以有很多评论.我在各个实体的帖子和评论之间建立了一对多的关系.当帖子显示在主页上时,我想在其下面有一个评论表单,用户可以在其中发表评论.为了发表评论,我必须在postID的表单中传递一个隐藏字段,但我无法这样做.以下是我的所有代码..

控制器:

<?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 …
Run Code Online (Sandbox Code Playgroud)

forms symfony

3
推荐指数
1
解决办法
9026
查看次数

Symfony 2隐藏表单字段文本

这可能真的很愚蠢,但我试图在symfony中设置一个隐藏的表单字段值,但是当我执行一个视图源时,该值不会显示出来.this-> postID是指我通过构造函数传递的值,但这并不重要,即使我将数据值设置为它未显示的字符串.我一定是在做一些非常愚蠢的事.这是我的表格..

public function buildForm(FormBuilder $builder, array $options)
    {
        $builder->add('text','text');
        $builder->add('IsshPost','hidden', array('data'=>$this->postID));
    }
Run Code Online (Sandbox Code Playgroud)

任何想法都错了吗?

symfony-forms symfony

3
推荐指数
1
解决办法
8284
查看次数

标签 统计

symfony ×2

forms ×1

symfony-forms ×1