symfony - Symfony 断言回调在管理类中不起作用
问题描述
@Assert\Callback
不起作用,我不明白为什么,我做错了什么?我想检查该字段中是否至少有一个值competitionTypes
。在项目的其他实体中,验证有效,但不适用于该实体。
实体:
/**
* @ORM\Table(name="experience_in_competition")
* @ORM\Entity()
* @Gedmo\SoftDeleteable(fieldName="deletedAt", timeAware=false)
*/
class ExperienceInCompetition
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @var string
* @Assert\NotBlank(
* message="value is invalid(field must be non empty)",
* groups={"KNS-2020-2"}
* )
*
* @ORM\Column(name="year", type="string", nullable=true)
*/
protected $year;
/**
* @ORM\ManyToMany(targetEntity="NKO\OrderBundle\Entity\Application\KNS\SecondStage2020\CompetitionType")
* @ORM\JoinTable(name="application_competition_type",
* joinColumns={@ORM\JoinColumn(name="application_id", referencedColumnName="id", onDelete="CASCADE")},
* inverseJoinColumns={@ORM\JoinColumn(name="competition_type_id", referencedColumnName="id")}
* )
*/
protected $competitionTypes;
public function __construct(string $year)
{
$this->setYear($year);
$this->competitionTypes = new ArrayCollection();
}
/**
* @Assert\Callback
*/
public function isValidCompetitionType(ExecutionContextInterface $context, $payload)
{
if ($this->getCompetitionType()->isEmpty()) {
$context->buildViolation('value is invalid(field must be non empty)')
->atPath('competitionTypes')
->addViolation();
}
}
}
管理员类:
class ExperienceInCompetitionAdmin extends AbstractAdmin
{
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->add('year', TextType::class, [
'label' => 'Год',
'required' => false,
'disabled' => true
])
->add('competitionType', EntityType::class, [
'class' => CompetitionType::class,
'label' => 'Конкурс',
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('u')
->andWhere('u.code = :name')
->setParameters(['name' => ApplicationTypes::KNS_APPLICATION_SECOND_STAGE_2020]);
},
'multiple' => true,
])
;
}
}
来自该实体的值未经过验证,我尝试将组插入@Assert\Callback
as for field $year
,但没有帮助。这种条目也无济于事:
* @Assert\Count(
* min="1",
* minMessage="value is invalid(field must be non empty)",
* groups={
* "KNS-2020-2"
* }
* )
解决方案
推荐阅读
- python - %matplotlib inline 和其他魔法在 windows 10(32 位)上的 jupyter notebook 中不起作用
- java - 等待连接方法执行然后移动到下一行 Retrofit Callback
- android - buildozer android 调试:c 编译器错误 - 无法创建可执行文件 - 配置:exit77
- angular - Angular 7拦截器更改标头并再次重试请求
- php - WordPress Error establishing a MySQL database connection IIS10
- sql - Oracle SQL how to make a number 279429429489284928942 to 279429429489284.928942 using SQL
- python - Send a message using message.content
- sql-server - 尽管存在索引但未解决的参考
- lua - 我可以从通过 string.dump 检索的 lua 字节码中得到什么?
- javascript - 单击按钮后如何将数据从数组插入数组