donglian1953
2016-04-30 14:58
浏览 55
已采纳

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

Using Symfony2.3.4, Doctrine2 and PHP5.6.3.

This is my validation.yml file:

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"

This is just a visual aid to help you understand what I need. This does not work how I want to. This:

When submitting the form:
- Current behavior:

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

Expected behavior:

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

Just in case, I have already accomplished this in my EntityName.php class with @UniqueEntity(fields={"f1", "f2"}, message="error") annotation, but I really need it in .yml.

Side quest:
Right now, with the annotation approach it is showing the error sign next to only f1, is there a way to show it next to all involved fields?

图片转代码服务由CSDN问答提供 功能建议

使用Symfony2.3.​​4,Doctrine2和PHP5.6.3。</ p>

这是 我的 validation.yml </ code>文件:</ p>

  Project \ NameBundle \ Entity \ EntityName:
 constraints:
  -  Symfony \ Bridge \ Doctrine \ Validator \  Constraints \ UniqueEntity:
字段:f1 
消息:“已经有一个具有相同f1的实体”
字段:f2 
消息:“已经有一个具有相同f2的实体”
 </ code>  </ pre> 
 
 

这只是一个视觉辅助工具,可以帮助您了解我的需求。 这不符合我的要求。 这:</ p>

提交表单时:
-当前行为:</ p>

  if(f2不是唯一的)
 错误; 
else 
提交; 
 </ code> </ pre> 
 
 

预期行为:</ p>

  if(f1 === X.  f1 AND f2 === X.f2)// X是已经在DB 
错误中的任何实体; 
else 
提交; 
 </ code> </ pre> 
 
 

Just 以防万一,我已经在我的 EntityName.php </ code>类中用 @UniqueEntity(fields = {“f1”,“f2”},message =“error”)</ code>完成了这个。 注释,但我真的需要它在 .yml </ code>。</ p>

Side quest:
现在,使用注释方法,它显示错误标志 只有 f1 </ code>,有没有办法在所有涉及的字段旁边显示它?</ p> </ div>

1条回答 默认 最新

相关推荐 更多相似问题