Reputation: 7696
I have an Entity with the field url
, this entity is being persisted to the database uppon sending a form based on the same entity.
I can set constraints to the form type in validation.yml
along with custom messages like NotBlank
and Url
. That's all fine. But when I try to persist an entity that already exists in the DB I get an error saying that the value already exists (url
).
How can I set a custom message for this UniqueEntity
constraint?
I tried adding UniqueEntity
in the validation.yml, but it is in a different namespace and I cannot understand how it works, or how to setup it. It is there by default, couse url
is defined as unique field in the original entity annotation, but how to change the error message??
Upvotes: 2
Views: 1944
Reputation: 7696
I just found a solution to my own problem.
All validations can be defined in validation.yml, but since I am using annotations to define my entities, it is better to put all validations, messages etc. inside those annotations.
It was enought to set @Assert\NotBlank
for a not blank field and @ORM\UniqueEntity(message="my custom error")
for the being unique validation.
I'm sorry for flooding the site with my question, but I hope someone might find this useful..
P.S.: Annotations or YML makes no difference, the setup is the same. I suppose in my case it should have been something like:
Namespace\Class:
constraints:
- Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity:
fields: url
message: "My custom message"
It is quite flexible, thought I don't see how we could set different messages if different fields are already present in the db...
Upvotes: 2