通过isValid方法进行表单验证始终在Symfony上返回false

时间:2016-05-20 23:00:33

标签: php forms symfony post

我有一个我的实体的表单类型,有一个输入字段和提交按钮。表单在视图中正确显示,但问题出现在我提交表单时,$form->isValid()总是返回false,我不知道原因。我打印发布请求的数据,我的数据在那里,我还有另一个表单类型的另一个实体,我没有任何问题。如果有人能告诉我isValid()方法是如何工作的,我将不胜感激因为我不明白这个函数是如何认为有效或无效的。

以下是我正在使用的代码:

Controller的代码:

public function commentAction(Request $request, $commentNumber){

    $com = new Comment();
    $em = $this->getDoctrine()->getManager();

    $comment = $em->getRepository('AppBundle:Comment')
    ->findById($commentNumber);

    $form = $this->createForm(new CommentType(), $com);

    $user = $this->get('security.context')->getToken()->getUser();
    $username = $user->getUsername();

    if($form->isValid()){
      $comment_object = $em->find('AppBundle:Comment', $commentNumber);

      $rep->setIdComment($comment_object);
      $rep->setUserReadReply('yes');
      $rep->setAdminReadReply('no');
      $rep->setReply($request->request->get('reply')['reply']);
      $rep->setUser($username);
      $em->persist($rep);
      $em->flush();
    }

    $comments = $em->getRepository('AppBundle:Comment')
    ->findByIdComment($commentNumber);
    $form->handleRequest($request);


    return $this->render('comments/see_comments.html.twig', array('comment' => $comment, 'comments' => $comments, 'form' => $form->createView()));
  }

CommentType的代码:

<?php

namespace AppBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\TextType;

class CommentType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('reply')
            ->add('send', 'submit');
        ;
    }

    /**
     * @param OptionsResolver $resolver
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'AppBundle\Entity\Comment'
        ));
    }
}

评论的分类:

<?php
namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;


/**
* @ORM\Table(name="comments")
* @ORM\Entity
* @ORM\Entity(repositoryClass="AppBundle\Repository\CommentRepository")
*/
class Comment {

  /**
  * @ORM\Id
  * @ORM\Column(type="integer")
  * @ORM\GeneratedValue(strategy="AUTO")
  */
  protected $id;

  /**
  * @ORM\ManyToOne(targetEntity="Post", inversedBy="posts")
  */
  protected $idPost;

  /**
  * @ORM\Column(type="string", length=255, nullable=true)
  */
  protected $user;

  /**
  * @ORM\Column(type="string", length=255, nullable=false)
  */
  protected $reply;

  /**
  * @ORM\Column(type="string", length=3, nullable=true)
  */
  protected $adminReadReply;

  /**
  * @ORM\Column(type="string", length=3, nullable=true)
  */
  protected $userReadReply;
    /**
     * Constructor
     */
    public function __construct()
    {
        $this->idTicket = new \Doctrine\Common\Collections\ArrayCollection();
    }

    /**
     * Get id
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set user
     *
     * @param string $user
     * @return Comment
     */
    public function setUser($user)
    {
        $this->user = $user;

        return $this;
    }

    /**
     * Get user
     *
     * @return string
     */
    public function getUser()
    {
        return $this->user;
    }

    /**
     * Set reply
     *
     * @param string $reply
     * @return Comment
     */
    public function setReply($reply)
    {
        $this->reply = $reply;

        return $this;
    }

    /**
     * Get reply
     *
     * @return string
     */
    public function getReply()
    {
        return $this->reply;
    }

    /**
     * Set adminReadReply
     *
     * @param string $adminReadReply
     * @return Comment
     */
    public function setAdminReadReply($adminReadReply)
    {
        $this->adminReadReply = $adminReadReply;

        return $this;
    }

    /**
     * Get adminReadReply
     *
     * @return string
     */
    public function getAdminReadReply()
    {
        return $this->adminReadReply;
    }

    /**
     * Set userReadReply
     *
     * @param string $userReadReply
     * @return Reply
     */
    public function setUserReadReply($userReadReply)
    {
        $this->userReadReply = $userReadReply;

        return $this;
    }

    /**
     * Get userReadReply
     *
     * @return string
     */
    public function getUserReadReply()
    {
        return $this->userReadReply;
    }

    /**
     * Set idPost
     *
     * @param \AppBundle\Entity\Post $idPost
     * @return Comment
     */
    public function setIdPost(\AppBundle\Entity\Post $idPost = null)
    {
        $this->idPost = $idPost;

        return $this;
    }

    /**
     * Get idPost
     *
     * @return \AppBundle\Entity\Post
     */
    public function getIdPost()
    {
        return $this->idPost;
    }
}

在see_comments.html.twig模板中,我有{{ form(form) }}(其余代码只有简单的html)

感谢所有

1 个答案:

答案 0 :(得分:1)

  

handleRequest方法从请求中获取POST数据,   处理它,并运行任何验证。

所以,你必须把:

$form->handleRequest($request); 

之前

if($form->isValid())