【问题标题】:How to override validation message of FOS bundle in symfony2如何在 symfony2 中覆盖 FOS 包的验证消息
【发布时间】:2014-03-21 02:40:52
【问题描述】:

我在我的项目中使用 FOS 包。我在我的管理包中创建了一个新的控制器ChangePasswordController。这是我的代码。

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())
  );
 }

}

现在我想自定义当前密码的错误信息。

【问题讨论】:

    标签: php mysql symfony-2.1


    【解决方案1】:

    您是否要覆盖 FOSUserBundle?见Documentation。如果你不是,那么你应该是,如果你想修改默认行为。

    如果是这样,那么您必须在覆盖 FOSUserBundle 的包中复制在 FOSUserBundle 供应商中找到的翻译文件(和父目录):

    Resources/translations/validators.xx.yml,其中 xx 是您的语言环境。

    然后更改错误消息。例如更改当前密码无效错误,写:

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

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 2014-11-01
      • 2012-08-28
      • 2014-08-01
      • 1970-01-01
      • 2018-01-31
      • 1970-01-01
      • 1970-01-01
      • 2020-01-17
      • 2018-01-28
      相关资源
      最近更新 更多