Symfony - 一个实体 属性 字段的多种形式约束
Symfony - Multiple form constraints on one entity property field
假设我有一个 Human
实体和 $surname
属性,并且我有两种表单类型,一种用于创建实体,一种用于在实体中搜索。
当我把注释 Assert/NotBlank()
放在姓氏 属性 上时,它也用于搜索表单。
有什么方法可以指定特定表单应该使用哪些约束注释,哪些不应该使用?
您可以从 class 中删除约束并在表单构建器中设置它,这将允许您对同一字段有不同的约束:
class CreateHumanType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add(
'surname',
TextType::class,
[
'label' => "surname",
'constraints' => [
new NotBlank(
[
'message' => "The surname is required",
]
),
],
]
);
}
//[...]
}
class SearchHumanType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add(
'surname',
TextType::class,
[
'label' => "surname",
'required' => "false",
]
);
}
//[...]
}
或者,您可以将约束保留在 class 中并将该字段设置为未针对搜索进行映射:
class SearchHumanType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add(
'surname',
TextType::class,
[
'label' => "surname",
'mapped' => false,
'required' => false,
]
);
}
//[...]
}
然后您必须在控制器中手动处理它。
使用注释,您还可以为实体约束分配一个验证组,如下例(使用注释):
/**
* @Assert/NotBlank(
* groups={"search_form"} <--- VALIDATION GROUP
* )
*/
protected $surname;
根据您的需要,您可以添加一个或多个验证组以在不同 forms/contexts 中使用特定断言,例如:groups={"registration", "edit", "search"}
然后你可以阅读文档(链接是最新版本)看看:
假设我有一个 Human
实体和 $surname
属性,并且我有两种表单类型,一种用于创建实体,一种用于在实体中搜索。
当我把注释 Assert/NotBlank()
放在姓氏 属性 上时,它也用于搜索表单。
有什么方法可以指定特定表单应该使用哪些约束注释,哪些不应该使用?
您可以从 class 中删除约束并在表单构建器中设置它,这将允许您对同一字段有不同的约束:
class CreateHumanType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add(
'surname',
TextType::class,
[
'label' => "surname",
'constraints' => [
new NotBlank(
[
'message' => "The surname is required",
]
),
],
]
);
}
//[...]
}
class SearchHumanType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add(
'surname',
TextType::class,
[
'label' => "surname",
'required' => "false",
]
);
}
//[...]
}
或者,您可以将约束保留在 class 中并将该字段设置为未针对搜索进行映射:
class SearchHumanType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add(
'surname',
TextType::class,
[
'label' => "surname",
'mapped' => false,
'required' => false,
]
);
}
//[...]
}
然后您必须在控制器中手动处理它。
使用注释,您还可以为实体约束分配一个验证组,如下例(使用注释):
/**
* @Assert/NotBlank(
* groups={"search_form"} <--- VALIDATION GROUP
* )
*/
protected $surname;
根据您的需要,您可以添加一个或多个验证组以在不同 forms/contexts 中使用特定断言,例如:groups={"registration", "edit", "search"}
然后你可以阅读文档(链接是最新版本)看看: