0

I use Respect Validation for password matches on a Slim app:

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

I can authenticate the 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
    }
}

...and I created my own custom string for the 3rd rule ('password_old'):

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

The script works fine, I get the following message when I submit with 'password_old' field empty:
"Password_old must not be empty"

I would like to change the above default message to a custom string, e.g.:
"The value must not be empty"

blsn
  • 1,077
  • 3
  • 18
  • 38

1 Answers1

0

You can overwrite the messages using the findMessages method of ValidationException and using 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);
}
Davide Pastore
  • 8,678
  • 10
  • 39
  • 53