如何验证实体变量,因为在我看来它对空选择有效。
/**
* @Assert\NotBlank(message = "education level cannot be empty")
* @var EducationLevel[]|ArrayCollection
* @ORM\ManyToMany(targetEntity="AppBundle\Entity\EducationLevel")
*/
private $educationLevel;
表单类型
->add('educationLevel', EntityType::class, [
'class' => 'AppBundle:EducationLevel',
'multiple' => true,
'choice_label' => function ($educationLevel) {
return $educationLevel->getName();
},
])
答案 0 :(得分:0)
答案 1 :(得分:-1)
您可以在实体中创建一个验证方法,该方法可以验证$ educationLevel是否为null或者是EducationLevel实例的集合。
/**
* @Assert\IsTrue(message="Education level has to be blank or...")
*/
public function isEducationLevelValid()
{
if ($this->educationLevel->isEmpty()) {
foreach ($this->educationLevel as $edulevel) {
if (!$edulevel instanceof EducationLevel) {
return false;
}
}
return true;
} else {
return false;
}
}
该方法在实体绑定表单提交期间自动使用,当然您可以将其用作普通实体的方法。