如何更改Respect Validation中的默认消息?

时间:2017-10-07 17:50:43

标签: php validation slim respect-validation

我在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
    }
}

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

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

脚本运行正常,当我提交 'password_old' 字段时,我收到以下消息:
Password_old不能为空

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

1 个答案:

答案 0 :(得分:0)

您可以使用ValidationException 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); } 方法并使用findMessages覆盖邮件:

Spring