Linux, WebDev and other…

Symfony form & doctrine – UniqueEntity validation and field not editable by user

Simple thing. One to many relation. (In my case: User to Sport).  User can not have related duplicated entries. (but entry name can be duplicated if different user) I’am working with symfony only few days and wasted some time on this easy task, so I writed this note.

UniqueEntity constraint is applied in entity:

User field is not selectable in form. Entity is taken from session (logged user). User cannot be set after form submit, because then, validation will not be fired. User entity must be injected to sport entity using form events. I used SUBMIT event. Events are explained here.

So, this is my form:


Leave a Reply

Your email address will not be published. Required fields are marked *