8000 excludeIf validation rule does not work as expected when using $this->validate from controller · Issue #1247 · laravel/lumen-framework · GitHub
[go: up one dir, main page]

Skip to content
excludeIf validation rule does not work as expected when using $this->validate from controller #1247
Closed
@MattApril

Description

@MattApril
  • 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0