symfony注释验证覆盖实体/模型

时间:2013-07-11 09:13:36

标签: validation symfony model annotations

我正在尝试覆盖论坛捆绑包的实体验证。 我是这样做的:

类别实体:

//src/MSD/ForoBundle/Entity/Category.php

namespace MSD\ForoBundle\Entity;

use Herzult\Bundle\ForumBundle\Entity\Category as BaseCategory;
use Doctrine\ORM\Mapping as ORM;


/**
 * @ORM\Entity(repositoryClass="Herzult\Bundle\ForumBundle\Entity\CategoryRepository")
 */
class Category extends BaseCategory
{
}

主题实体:

//src/MSD/ForoBundle/Entity/Topic.php

namespace MSD\ForoBundle\Entity;

use Herzult\Bundle\ForumBundle\Entity\Topic as BaseTopic;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * Topic
 *
 * @ORM\Entity(repositoryClass="Herzult\Bundle\ForumBundle\Entity\TopicRepository")
 *
 */
class Topic extends BaseTopic
{
/**
 * @ORM\ManyToOne(targetEntity="Category")
 */
protected $category;

/**
 * @Assert\NotBlank()
 * @Assert\MinLength(limit=4, message="Just a little too short| ")
 * @Assert\Regex(
* pattern="/^[a-zA-Z0-9\-_¿?!¡ ]{4,50}$/",
* message="El tema puede contener letras, números, guiones y espacios, interrogantes y exclamaciones. Entre 4 y 30 caracteres"
* )
*/
protected $subject;

/**
 * {@inheritDoc}
 */
public function getAuthorName()
{
    return $this->author;
}

/**
 * @ORM\ManyToOne(targetEntity="User")
 */
private $author;

public function setAuthor(User $user)
{
    $this->author = $user;
}

public function getAuthor()
{
    return $this->author;
}
}

发布实体:

//src/MSD/ForoBundle/Entity/Post.php

namespace MSD\ForoBundle\Entity;

use Herzult\Bundle\ForumBundle\Entity\Post as BasePost;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @ORM\Entity(repositoryClass="Herzult\Bundle\ForumBundle\Entity\PostRepository")
 */
class Post extends BasePost
{
/**
 * @ORM\ManyToOne(targetEntity="Topic")
 */
protected $topic;

 /**
 * @Assert\Regex(
* pattern="/^[^<>]{4,1000}$/",
* message="El mensaje no puede contener '<' ni '>'. Entre 4 y 1000 caracteres"
* )
 *
 */
public $message;

public function getAuthorName()
{
    return $this->author;
}
/**
 * @ORM\ManyToOne(targetEntity="User")
 */
private $author;

public function setAuthor(User $user)
{
    $this->author = $user;
}

public function getAuthor()
{
    return $this->author;
}
}

验证工作......除了第一个帖子的消息!!创建新主题时创建的。 我尝试了很多改变,但没有成功。 知道为什么会这样吗?

谢谢

1 个答案:

答案 0 :(得分:0)

呀!我知道了。解决方案是在Topic实体中添加它:

/**
 * @Assert\NotBlank
 * @Assert\Valid
 */
protected $firstPost;

然后,验证第一篇文章的消息。