我有一个带有组合框的表单,当提交时,不能重复这些点数值。
形式
->add('points', 'choice', array(
'attr' => array('class' => 'form-control m-b'),
'choices' => array(
'0' => '0',
'1' => '1',
'2' => '2',
'3' => '3',
'4' => '4',
'5' => '5',
'6' => '6',
'7' => '7',
'8' => '8',
'9' => '9',
'10' => '10',
)
))symfony的版本是2.6
如有任何帮助,将不胜感激。
发布于 2016-01-08 14:29:26
为此您需要一个自定义验证,您可以使用回调创建一个自定义验证,如下所示。
假设点属性为数组
实体类上的
如果您使用注释作为验证格式:
class Author
{
private $points;
/**
* @Assert\Callback
*/
public function validate(ExecutionContextInterface $context)
{
if(sizeof(array_unique($this->points)) !== sizeof($this->points)){
$context->buildViolation('Values must be unique')
->atPath('points')
->addViolation();
}
}
}如果您使用YAML作为验证格式,则在本例中需要从上述验证函数中删除:
/**
* @Assert\Callback
*/..。
# src/AppBundle/Resources/config/validation.yml
AppBundle\Entity\Author:
constraints:
- Callback: [validate]有关回调的更多信息,请参阅Symfony回调
https://stackoverflow.com/questions/34677438
复制相似问题