I have the following validations defined for ProductType, ProductTypeAttributes
(OneToMany):
Bike\ProductBundle\Entity\ProductType:
properties:
name:
- NotBlank: ~
productTypeAttributes:
- Valid: ~
Bike\ProductBundle\Entity\ProductTypeAttribute:
constraints:
- Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity:
fields: [attribute, productType]
errorPath: attribute
message: 'Attributes must be different for the same product type.'
There is a unique key for ProductTypeAttributes
: attribute,productType
.
I am using embedded forms (collection type) for ProductTypeAttributes
with the possibility to add/remove items. The validation seems to work only for the already existent records in the db, happening only if I am adding a new related entity which will trigger the unique key violation for a record.
The problem is the validation doesn't work when adding two completely new related entities, with the same attribute/productType. In this case I get the "duplicate entry" exception.
So the validation checks only through db records using default findBy
method, but not against newly added records themselves for duplicates.
Any way to overcome this?