I have an entity class with two properties, country and state. The entries in the database have to be unique. Meaning, if I submit country=Germany and state=BB to the database, it adds the new entry if the same entry does not yet exist.
However, the state property can be null, in that case it is not mapped by doctrine(?) and I can still submit a value for the country. The problem is that I cannot manage to make this entry unique i.e. you should not be able to input the same country multiple times without a unique state. Here is where I struggle.
I have tried various modifications to the entity constraints.
Here is the code that verifies that country & state are an unique entry in the database:
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addConstraint(new UniqueEntity([
'fields' => ['country', 'state'],
'message' => 'State is already used with this country!',
]));
}