Framework: [FormRequest] withValidator() method is called when the validation fails.

Created on 23 Sep 2018  路  10Comments  路  Source: laravel/framework

  • Laravel Version: 5.7.5
  • PHP Version: 7.2.9
  • Database Driver & Version: MySQL 5.7.23

Description:

Method withValidator() is called incorrectly. Previously, it worked only when all rules were successfully validated. Now it is always called, even when validation fails. Thus, the parameters placed in this method from the query are undefined and cause an error.

Steps To Reproduce:

  1. Controller:
use App\Http\Requests\BookingStoreRequest;

class BookingController extends Controller
{
    public function store(BookingStoreRequest $request)
    {
        $this->bookingService->createBooking($request->validated());

        return response()->json(['message' => 'Booking was successfully created!']);
    }
}
  1. FormRequest:
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class BookingStoreRequest extends FormRequest
{
    public function rules()
    {
        return [
        'check_in' => 'required|date_format:Y-m-d|before_or_equal:check_out',
        'check_out' => 'required|date_format:Y-m-d|before_or_equal:check_in',
        'info' => 'max:200'
        ];
    }

    public function withValidator($validator)
    {
        $validator->after(function ($validator){
            if(!$this->checkAvailable($this->input(['check_in', 'check_out']))){
                $validator->errors()->add('unavailable', 'The dates you selected are busy!');
            }
        });
    }
}

Most helpful comment

My solution of the problem now:

public function withValidator($validator)
{
    if(!$validator->fails()){
        $validator->after(function ($validator){
            if(!$this->checkAvailable($this->input(['check_in', 'check_out']))){
                $validator->errors()->add('unavailable', 'The dates you selected are busy!');
            }
        });
    }
}

Now it's working.

All 10 comments

When was it last working?

Before 8af60026af486834e77f93b0789c8747788ff7fb commit.

/cc @ttsuru

refs #25736
validated() method causes the validation to be called again.
"Steps To Reproduce:" is not reproduce with this step alone.

@staudenmeir @DmitryNiko
Not before 8af6002. This cause 941c8c7.

@DmitryNiko

If your BookingStoreRequest overwrite validated method to following, is it work?

    public function validated()
    {
        $rules = $this->getValidatorInstance()->getRules();

        return $this->only(array_keys($rules));
    }

No, overwriting the method doesn't help. Same things.

No, overwriting the method doesn't help. Same things.

Is this true?
If your method overwrite this, It will be work.
And if # 25736 is fixed, The above is unnecessary.

Now it is always called, even when validation fails.

This is false. validated() method causes the validation to be called again.

My solution of the problem now:

public function withValidator($validator)
{
    if(!$validator->fails()){
        $validator->after(function ($validator){
            if(!$this->checkAvailable($this->input(['check_in', 'check_out']))){
                $validator->errors()->add('unavailable', 'The dates you selected are busy!');
            }
        });
    }
}

Now it's working.

Will be fixed once 5.8 is released.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

shopblocks picture shopblocks  路  3Comments

YannPl picture YannPl  路  3Comments

digirew picture digirew  路  3Comments

Fuzzyma picture Fuzzyma  路  3Comments

kerbylav picture kerbylav  路  3Comments