I am building a custom validator. In a controller I would get the security context using:
$this->get('security.context')->getToken()->getUser()
I can’t manage to do the same in a custom validator; could anyone help me out?
Thanks so much!
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You either need to extend ContainerAware with your filter class then call setContainer on the class when you instantiate it or pass through the container to the class on construct. This is because filters are not container aware by default like the controller is.
To do it by constructor you can add the __construct method like below to your filter class:
Either way you will need to use container in your call:
Check this link https://groups.google.com/forum/?fromgroups#!topic/symfony2/WWIc8N7KZrg for extending container aware idea for doctrine entities, might be of some help.
Edit
Looking at the docs for validator you can add a service if it has any dependencies:
why not add an argument to that for the container:
and then use the construct method?