dingo / api

A RESTful API package for the Laravel and Lumen frameworks.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

FormRequest->withValidator is never getting called

sudkumar opened this issue · comments

Q A
Bug? yes
New Feature? no
Framework Lumen
Framework version 5.8.0
Package version 2.2.3
PHP version 7.2.14

Related to Dingo\Api\Http\FormRequest

Actual Behaviour

withValidator function gives us the ability the customise the validator instance before evaluating the rules. But withValidator is never getting called, because in LumenServiceProvider, we are directly calling the $resolved->validate() which never calls the FormRequest->getValidatorInstance method.

Expected Behaviour

withValidator should be called before evaluating the rules.

Steps to Reproduce

I am using the withValidator function to validate the current_password field when a Change Password request comes.

<?php
namespace App\Http\Requests;

use Dingo\Api\Http\FormRequest;
use Illuminate\Validation\Validator;

class ChangePasswordRequest extends FormRequest
{
  public function authorize(): bool {
    return true;
  }

  public function rules(): array
  {
    return ["current" => "required|string", "password" => "required|string|confirmed"];
  }

  public function withValidator (Validator $validator): void
  {
    $validator->after(function ($validator) {
      if (!app('hash')->check($this->current, $this->user()->password)) {
        $validator->errors()->add("current", "Current password is incorrect");
      }
    });
  }
}

Here, withValidator function is never getting called and validation passes even when the current password in not correct.

Possible Solutions

As described in FormRequestServiceProvider, we should call $resolved->validateResolved() in the LumenServiceProvider.

commented

Resolved in above PR.