Scaramouche
Scaramouche

Reputation: 3257

unique entity constraint using SEVERAL properties instead of one in 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?

Upvotes: 0

Views: 795

Answers (1)

Miro
Miro

Reputation: 1899

Documentation (http://symfony.com/doc/current/reference/constraints/UniqueEntity.html#fields) says that fields can be array:

fields: [f1, f2]

YML format is described for example here: http://symfony.com/doc/current/components/yaml/yaml_format.html

Upvotes: 1

Related Questions