Symfony 登录FOSUserBundle后的自定义错误消息

Symfony 登录FOSUserBundle后的自定义错误消息,symfony,security,authentication,fosuserbundle,Symfony,Security,Authentication,Fosuserbundle,我正在使用FOSUserBundle和Symfony 3.4来登录我的用户。当他们输入错误的密码时,将显示以下消息: 我想添加对用户类型的检查,并添加自定义错误消息,例如:“您必须是客户才能登录。” 为了完成此任务,我实现了一个用户检查器,但它没有按预期工作: class CustomerUserChecker implements UserCheckerInterface { public function checkPreAuth(UserInterface $user)

我正在使用FOSUserBundle和Symfony 3.4来登录我的用户。当他们输入错误的密码时,将显示以下消息:

我想添加对用户类型的检查,并添加自定义错误消息,例如:“您必须是客户才能登录。”

为了完成此任务,我实现了一个用户检查器,但它没有按预期工作:

class CustomerUserChecker implements UserCheckerInterface
{
    public function checkPreAuth(UserInterface $user)
    {
        // nothing to do here
        return;
    }

    public function checkPostAuth(UserInterface $user)
    {
        if (!$user instanceof CustomerUser) {
            throw new AuthenticationException('You must be a customer in order to login');
        }

        return;
    }
}
我得到这个错误:


如何在文本中添加新错误?

我使用自定义异常实现了目标:

<?php

namespace AppBundle\Security;

use Symfony\Component\Security\Core\Exception\AccountStatusException;

class CustomerUserException extends AccountStatusException
{
    /**
     * {@inheritdoc}
     */
    public function getMessageKey()
    {
        return 'You must be a customer in order to login.';
    }
}
并正确显示信息:


另一种方法是将
validators.en.yml
…供应商/朋友SYMFONY/user bundle/Resources/translations
复制到
…app/Resources/translations
,以覆盖它。在那里进行任何所需的更改。

如果您抛出一个
CustomUserMessageAuthenticationException
而不是
AuthenticationException

是,它将起作用,但这只会覆盖一条错误消息。我想添加新的错误消息。
<?php

namespace AppBundle\Security;

use Application\Sonata\UserBundle\Entity\CustomerUser;
use Application\Sonata\UserBundle\Entity\User;
use Symfony\Component\Security\Core\User\UserCheckerInterface;
use Symfony\Component\Security\Core\User\UserInterface;

class CustomerUserChecker implements UserCheckerInterface
{
    public function checkPreAuth(UserInterface $user)
    {
        // nothing to do here
        return;
    }

    public function checkPostAuth(UserInterface $user)
    {
        if (!$user instanceof User) {
            return;
        }

        if (!$user instanceof CustomerUser) {
            throw new CustomerUserException();
        }
    }
}
<?xml version="1.0"?>
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
    <file source-language="en" datatype="plaintext" original="file.ext">
        <body>
            <trans-unit id="You must be a customer in order to login.">
                <source>You must be a customer in order to login.</source>
                <target>You must be a customer in order to login.</target>
            </trans-unit>
        </body>
    </file>
</xliff>