fos user bundle security validation

A login authentication would be handled by securitybundle in symfony.

But how about if you need to validate or authenticate username and password from controller?

Here is what you can do

        public function authenticatememberAction($user, $password)
           $valid = false;

           $user_manager = $this->get('fos_user.user_manager');
           $factory = $this->get('security.encoder_factory');

           $user = $user_manager->findUserByUsername($user);//get the user first
           if (!empty($user)) {
              $encoder = $factory->getEncoder($user); //then the encoder
              $valid = ($encoder->isPasswordValid($user->getPassword(),$password,$user->getSalt())) ? true : false;

           return new JsonResponse(['valid'=>$valid]);

The above snippet will validate if the provided username password combination is correct or not.