Symfony 自定义验证约束传递参数
Symfony Custom Validation Constraint pass parameter
我有一个自定义验证约束来检查表单中输入的书名是否唯一。
我不能使用@Unique,因为这也会验证书的当前值并且总是会抛出验证错误,即使我不想更改书名。
(如果有一种方法可以忽略当前值,我很想听听,但我仍然想知道如何将自定义对象传递给自定义验证器:))
所以我为书实体的名称属性写了这个:
/**
* @ORM\Entity
* @UniqueEntity("name", groups={"Creation"})
*/
public class Book {
/*
* @var string
* @ORM\Column(type="string", unique = true)
* @CustomAssert\UniqueBookName(groups={"Editmode"})
*/
protected $name;
}
为此,我需要我想要在 UniqueBookName Validator class 中验证的当前书名。
现在如何将当前书名传递给我的验证器 class 在这一行中:
@CustomAssert\UniqueBookName(groups={"Editmode"})
我知道如何在这里传递参数:
@CustomAssert\UniqueBookName(exampleProperty="name", groups={"Editmode"})
但是我怎样才能通过,例如exampleProperty 当前书名的真实值,不是字符串"name"?
此致
答案更新:
我的Class约束:
/**
*@CustomAssert\UniqueBookName(nameProperty="name", groups={"Editmode"})
*/
class Book{
// Property as you described
}
我的验证器 classes:
class UniqueBookName extends Constraint{
public $message = 'book.edit.name';
public $nameProperty;
public function getDefaultOption()
{
return 'nameProperty';
}
public function getRequiredOptions()
{
return array('nameProperty');
}
public function validatedBy()
{
return 'bookName_validator';
}
public function getTargets()
{
return self::CLASS_CONSTRAINT;
}
}
以及对应的验证器"validate"-Method:
public function validate($value, Constraint $constraint)
{
// here, in $constraint I only see the String Value "name", not the current value I want.
}
我喜欢将虚拟资源附加到表单,作为完成此类验证的一种方式。
构建 Book 模型时,只需为它分配一个新的 属性 和一个图书记录实例。然后,您可以将验证器应用为 class 约束(而不是基于 属性),这样您就可以访问整个模型。
$book = new Book();
$book->oldBook = $repository->fetchBook($id);
$form ...
作为使该模型尽可能可重用的良好做法,我另外将属性名称指定为约束的参数。然后使用 property accessor,您可以检索这些值。几乎完全像 UniqueEntity class 验证器指定 "name" 属性.
/**
* @ORM\Entity
* @CustomAssert\UniqueBookName(bookProperty="oldBook", bookNameProperty="name", groups={"Editmode"})
* @UniqueEntity("name", groups={"Creation"})
*/
public class Book {
/*
* @var string
* @ORM\Column(type="string", unique = true)
*/
protected $name;
}
我有一个自定义验证约束来检查表单中输入的书名是否唯一。 我不能使用@Unique,因为这也会验证书的当前值并且总是会抛出验证错误,即使我不想更改书名。
(如果有一种方法可以忽略当前值,我很想听听,但我仍然想知道如何将自定义对象传递给自定义验证器:))
所以我为书实体的名称属性写了这个:
/**
* @ORM\Entity
* @UniqueEntity("name", groups={"Creation"})
*/
public class Book {
/*
* @var string
* @ORM\Column(type="string", unique = true)
* @CustomAssert\UniqueBookName(groups={"Editmode"})
*/
protected $name;
}
为此,我需要我想要在 UniqueBookName Validator class 中验证的当前书名。
现在如何将当前书名传递给我的验证器 class 在这一行中:
@CustomAssert\UniqueBookName(groups={"Editmode"})
我知道如何在这里传递参数:
@CustomAssert\UniqueBookName(exampleProperty="name", groups={"Editmode"})
但是我怎样才能通过,例如exampleProperty 当前书名的真实值,不是字符串"name"?
此致
答案更新:
我的Class约束:
/**
*@CustomAssert\UniqueBookName(nameProperty="name", groups={"Editmode"})
*/
class Book{
// Property as you described
}
我的验证器 classes:
class UniqueBookName extends Constraint{
public $message = 'book.edit.name';
public $nameProperty;
public function getDefaultOption()
{
return 'nameProperty';
}
public function getRequiredOptions()
{
return array('nameProperty');
}
public function validatedBy()
{
return 'bookName_validator';
}
public function getTargets()
{
return self::CLASS_CONSTRAINT;
}
}
以及对应的验证器"validate"-Method:
public function validate($value, Constraint $constraint)
{
// here, in $constraint I only see the String Value "name", not the current value I want.
}
我喜欢将虚拟资源附加到表单,作为完成此类验证的一种方式。
构建 Book 模型时,只需为它分配一个新的 属性 和一个图书记录实例。然后,您可以将验证器应用为 class 约束(而不是基于 属性),这样您就可以访问整个模型。
$book = new Book();
$book->oldBook = $repository->fetchBook($id);
$form ...
作为使该模型尽可能可重用的良好做法,我另外将属性名称指定为约束的参数。然后使用 property accessor,您可以检索这些值。几乎完全像 UniqueEntity class 验证器指定 "name" 属性.
/**
* @ORM\Entity
* @CustomAssert\UniqueBookName(bookProperty="oldBook", bookNameProperty="name", groups={"Editmode"})
* @UniqueEntity("name", groups={"Creation"})
*/
public class Book {
/*
* @var string
* @ORM\Column(type="string", unique = true)
*/
protected $name;
}