Done!

Creating Custom Validation in Laravel 5


First create a class that extends Validator (\App\Http\Validator). Prefix the method name with validate and pass the parameters as shown in the validateCheckHashPassword() function below:

namespace App\Http\Validator;

use App;
use Illuminate\Validation\Validator;
use Illuminate\Contracts\Hashing\Hasher as HasherContract;

class HashPasswordValidator extends Validator
{
    public function validateCheckHashPassword($attribute, $value, $parameters, $validator)
    {   
        $hasher = App::make(HasherContract::class);
   
        if (!$hasher->check(request()->password, auth()->user()->getAuthPassword())) {            
            return false;
        }
        
        return true;
    }
}

Use existing AppServiceProvider, or create a new Validation service provider to boot the custom validation class created in the previous step.

namespace App\Providers;

use Validator;
use Illuminate\Support\ServiceProvider;
use App\Http\Validator\HashPasswordValidator;

class ValidationServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        $this->hashPasswordValidator();
    }

    private function hashPasswordValidator()
    {
        Validator::resolver(function($translator, $data, $rules, $messages)
        {
            return new HashPasswordValidator($translator, $data, $rules, $messages);
        });
    }

    public function register() {}
}
 

If you choose to use dedicated ValidationServiceProvider, ensure you register your provider.

'providers' => [
    ...

    Illuminate\View\ViewServiceProvider::class,
    
    ...
]

That Done! You can now use the custom validation on a Request class as shown below:

    public function rules()
    {
        return [
            'password' => 'checkHashPassword',
        ];
    }