0

我在 Slim 应用程序上使用 Respect Validation 进行密码匹配:

class PasswordController extends Controller
{
    ;
    ;
    public function postChangePassword($request, $response) { 
        $validation = $this->validator->validate($request, [
            'password_old' => v::noWhitespace()->notEmpty()->matchesPassword($this->auth->user()->password),
            'password' => v::noWhitespace()->notEmpty()
        ]);

        if($validation->failed()) { 
            // stay on the same page
        }

        die('update password');
    }
}

我可以验证密码:

class MatchesPassword extends AbstractRule 
{
    protected $password;

    public function __construct($password) { 
        $this->password = $password;
    }

    public function validate($input) { 
        // compare the non-hashed input with the already hashed password
    }
}

...我为第三条规则('password_old')创建了自己的自定义字符串:

class MatchesPasswordException extends ValidationException 
{
    public static $defaultTemplates = [
        self::MODE_DEFAULT => [
            self::STANDARD => 'Password does not match.',
        ],
    ];
}

该脚本工作正常,当我在 “password_old”字段为空的情况下提交时收到以下消息:
Password_old 不得为空

我想将上面的默认消息更改为自定义字符串,例如:
该值不能为空

4

1 回答 1

0

findMessages您可以使用以下方法覆盖消息ValidationException并使用assert

try {
    v::noWhitespace()->notEmpty()->matchesPassword($this->auth->user()->password)->assert($request->getParam('password_old'));
    v::noWhitespace()->notEmpty()->assert($request->getParam('password'));
} catch (ValidationException $exception) {
    $errors = $exception->findMessages([
        'notEmpty' => 'The value must not be empty'
    ]);
    print_r($errors);
}
于 2017-11-19T00:56:00.220 回答