将断言信息添加到symfony命令行生成的CRUD实体

时间:2014-06-10 12:55:40

标签: symfony entity assertions

生成实体时是否可以在命令行中添加Assert需求?换句话说,当我运行“../app/console doctrine:generate:entity ...”时,如何添加信息,使实体具有此处显示的断言类型:

/**
 * 
 * @var string $PID
 * @ORM\Column(name="PID", type="string", length=50)
 * 
 * 
 * @Assert\Regex(
 *   pattern="/^(\d{7,7}[Aa])$|^(\d{8,8})$/",
 *   message="Format Must Be 8 digets Or 7 Digits Followed By 'A'"
 * )
 */
private $PID;

1 个答案:

答案 0 :(得分:0)

您仍然可以使用验证组件:

http://symfony.com/doc/current/book/validation.html

只需使用它来验证实体的创建时间:

$validator = $this->get('validator'); // Or wherever you can get services from
$errors    = $validator->validate($entity);

if (count($errors) > 0) {
    // Handle errors here...
} else {
    // Success!
}