使用SEVERAL属性而不是validation.yml symfony2中的一个属性的唯一实体约束


unique entity constraint using SEVERAL properties instead of one in validation.yml symfony2

使用Symfon2.3.4、Doctrine2和PHP5.6.3。

这是我的validation.yml文件:

Project'NameBundle'Entity'EntityName:
    constraints:
        - Symfony'Bridge'Doctrine'Validator'Constraints'UniqueEntity:
            fields: f1
            message: "There is already an entity with the same f1"
            fields: f2
            message: "There is already an entity with the same f2"

这只是一个视觉帮助,帮助你了解我需要什么。这不是我想要的工作方式。这个:

提交表格时:
-当前行为:

if(f2 is not unique)
   error;
else
   submits;

预期行为:

if(f1 === X.f1 AND f2 === X.f2)  //X is any of the entities already in the DB
   error;
else
   submits;

以防万一,我已经在EntityName.php类中用@UniqueEntity(fields={"f1", "f2"}, message="error")注释完成了这一点,但我确实需要在.yml中使用它。

附带任务:
现在,使用注释方法,它只在f1旁边显示错误符号,有没有办法在所有相关字段旁边显示它?

文档(http://symfony.com/doc/current/reference/constraints/UniqueEntity.html#fields)表示字段可以是数组:

fields: [f1, f2]

YML格式描述如下:http://symfony.com/doc/current/components/yaml/yaml_format.html