简体   繁体   中英

How to override validation message of FOS bundle in symfony2

I am using FOS bundle in my project. I created a new controller ChangePasswordController in my admin bundle. Here is my code.

namespace Pondip\AdminBundle\Controller;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\RedirectResponse;
use FOS\UserBundle\Controller\ChangePasswordController as BaseController;

/**

Controller managing the change password *
@author Aman
/
class ChangePasswordController extends BaseController
{
/**
This action is used for change password for admin.
@author Aman
@return render view
@throws AccessDeniedException as exception
@
 */
public function changePasswordAction()
{  
  $user = $this->container->get('security.context')->getToken()->getUser();


  $form = $this->container->get('fos_user.change_password.form');
  $formHandler = $this->container->get('fos_user.change_password.form.handler');

  $process = $formHandler->process($user);
  if ($process) {
      $this->setFlash('fos_user_success', 'Password has been changed successfully');

      $url = $this->container->get('router')->generate('pondip_admin_changepassword');
      return new RedirectResponse($url);
  }

  return $this->container->get('templating')->renderResponse(          
      'PondipAdminBundle:ChangePassword:changePassword.html.'.$this->container->getParameter('fos_user.template.engine'),
      array('form' => $form->createView())
  );
 }

}

Now I want to customize error message for current password.

Are you overriding the FOSUserBundle? See Documentation . If you're not, then you should be, if you want to modify default behaviors.

If so, you then have to copy, in your bundle that is overriding the FOSUserBundle, the translation file (and parent directories) found in the FOSUserBundle vendor :

Resources/translations/validators.xx.yml where xx is your locale.

Then change the error messages. For example, to change the current password invalid error, write :

fos_user:
    [...]
    current_password:
        invalid: Your own error message here
    [...]

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM