我在Symfony 3中有一个表单,我希望用户上传文件或填写评论(或两者)。
$form = $this->createFormBuilder(
$approvalRequest,
array(
'validation_groups' => array('revocation_proposal')
)
)
->add(
'deletionComment',
TextareaType::class,
array(
'attr' => array(
'cols' => 70,
'rows' => 10
),
'required' => false //otherwise html will force it to be required
)
)
->add('deletionTemplate',
ResearchTemplateType::class,
array('label' => 'Deletion Form',
'required' => false)) //otherwise html will force it to be required
->add(
'cancel',
SubmitType::class,
array(
'label' => 'Cancel'
)
)
->add('revoke', SubmitType::class, array(
'label' => 'Revoke Approval',
'attr' => array('class' => 'btn-danger')
))
->getForm();
$form->handleRequest($request);
我正在努力强制要求两个中的至少一个(评论或文件)必须存在,以便使用callbacks使表单有效。
在我的实体中,我有:
/**
* @Assert\Callback
*/
public function validate(ExecutionContextInterface $context)
{
if (is_null($this->getDeletionComment()) && is_null($this->getDeletionTemplate())) {
$context->buildViolation('You must write a deletion comment or upload a deletion template')
->atPath('deletionTemplate')
->addViolation();
}
}
但它没有被召集。我错过了什么吗?
由于