laravel / lumen-framework

The Laravel Lumen Framework.
https://lumen.laravel.com
MIT License
1.48k stars 420 forks source link

excludeIf validation rule does not work as expected when using $this->validate from controller #1247

Closed MattApril closed 2 years ago

MattApril commented 2 years ago

Description:

According to Laravel docs:

The field under validation will be excluded from the request data returned by the validate and validated methods.

In Lumen, this is not the case, and there is nothing stating so in the docs. It does respect the rule when applying other validation rules (a non-numeric value could but submitted in the example below), but it does not respect it when returning the validated data.

Steps To Reproduce:

// $request->field1 = 100
$validated = $this->validate($request, [
    'field1' => [Rule::excludeIf(true), 'numeric']
]);

var_dump(isset($validated['field1']));

Expected output: FALSE Actual output: TRUE

Suggestion

I dug a bit deeper and see that Laravel\Lumen\Routing\ProvidesConvenienceMethods has its own method extractInputFromRules, which clearly does not factor in the exclude_if validation rule at all. My question is, why not just simplify the validate method to do exactly what Laravel does:

public function validate(Request $request, array $rules, array $messages = [], array $customAttributes = []) {
        return $this->getValidationFactory()->make($request->all(), $rules, $messages, $customAttributes)->validate();
}

I overrode the method in my application and all my tests are still passing. I have to assume it was done differently in Lumen for a reason, but I'm happy to submit a pull request if the above is acceptable.

driesvints commented 2 years ago

My question is, why not just simplify the validate method to do exactly what Laravel does:

Yes, feel free to send in a PR, thanks!