Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
MattApril opened this issue Sep 8, 2022 · 1 comment

Comments

@MattApril
Copy link
Contributor

MattApril commented Sep 8, 2022

  • Lumen Version: 9.0.2
  • Laravel Version: 9.17.0
  • PHP Version: 8.1.6

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
Copy link
Member

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!

MattApril added a commit to MattApril/lumen-framework that referenced this issue Sep 9, 2022
taylorotwell added a commit that referenced this issue Sep 12, 2022
… (#1248)

* Change controller validate helper method to behave like Laravel (#1247)

* Set response on validation exception, which is expected by exception handler.

* Remove used import

* style conformity

* Add backwards compatibility for removed methods: extractInputFromRules and throwValidationException

* Cleanup

* code style

* Update ProvidesConvenienceMethods.php

Co-authored-by: Taylor Otwell <taylor@laravel.com>
driesvints added a commit that referenced this issue Sep 15, 2022
taylorotwell pushed a commit that referenced this issue Sep 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants