验证相关实体,至少一个
Validate a related entity, at least one
我有两个实体,"Page" 和 "Sections"。实体 "Page" 与实体 "Section" 具有一对多关系。
我创建了一个包含页面和部分集合的表单。我想验证一个页面是否至少有一个部分。
我在我的验证 yml 文件中尝试这个。
App\MyBundle\Entity\Page:
properties:
sections:
- Valid: ~
是否存在可用于处理此问题的约束?或者我必须为这个问题写一个自己的验证器吗?
如果您想查看您的主页至少有一个 Section
,那么您必须查看 Collection
部分是否大于 0。
App\MyBundle\Entity\Page:
properties:
sections:
- Count: { min: 1, minMessage: "You need to have at least one Section" }
如果您想查看您的 Section 对象是否也有效,那么您需要为您的 Section 实体和 PageType 表单集设置验证策略 cascade_validation => true
赞:
App\MyBundle\Entity\Section:
properties:
name:
- NotBlank
还有你的 FormType
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'App\MyBundle\Entity\Page',
'cascade_validation' => true,
));
}
我有两个实体,"Page" 和 "Sections"。实体 "Page" 与实体 "Section" 具有一对多关系。
我创建了一个包含页面和部分集合的表单。我想验证一个页面是否至少有一个部分。
我在我的验证 yml 文件中尝试这个。
App\MyBundle\Entity\Page:
properties:
sections:
- Valid: ~
是否存在可用于处理此问题的约束?或者我必须为这个问题写一个自己的验证器吗?
如果您想查看您的主页至少有一个 Section
,那么您必须查看 Collection
部分是否大于 0。
App\MyBundle\Entity\Page:
properties:
sections:
- Count: { min: 1, minMessage: "You need to have at least one Section" }
如果您想查看您的 Section 对象是否也有效,那么您需要为您的 Section 实体和 PageType 表单集设置验证策略 cascade_validation => true
赞:
App\MyBundle\Entity\Section:
properties:
name:
- NotBlank
还有你的 FormType
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'App\MyBundle\Entity\Page',
'cascade_validation' => true,
));
}