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.
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!']);
}
}
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!');
}
});
}
}
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.
Most helpful comment
My solution of the problem now:
Now it's working.