Muhammad Taqi
Muhammad Taqi

Reputation: 5424

Symfony2 Case sensitive Unique validation on 2 fields

beginner to symfony, i have a calss institutes

class Institutes {
    protected $id;
    protected $name;
    protected $city;
}

what i want the name of the institute name unique in each city. In other words preventing users to input multiple institutes in same city. I know that there is a unique entity option but that can't help. because institute name can be same in different cities but not in the same city.

Some thing i read about custom validation using repository classes, but don't konw how will it work. e.g

      name       |   city   | valid  |
-----------------+-------------------+
DHQ Hospital    | Attock    | t      |
dhq Hospital    | Attock    | f      | already exists in the city
dHQ HospITal    | Attock    | f      | already exists in the city
DHQ Hospital    | Islamabad | t      |

Upvotes: 0

Views: 725

Answers (1)

Carlos Granados
Carlos Granados

Reputation: 11351

Use the UniqueEntity validator, it can check that a combination of fields, not just a single field is unique. In your case, you could use (using annotations):

use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;

/**
 * @ORM\Entity
 * @UniqueEntity(fields = {"name", "city"})
 */
class Institutes
{
...
}

Upvotes: 1

Related Questions