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

[5.3] Allow to hook into the validator instance from the FormRequest #15918

Merged
merged 2 commits into from
Oct 15, 2016

Conversation

shadoWalker89
Copy link
Contributor

Before this PR, when i need to add an After Validation Hook in a FormRequest class i must define a validator() method

public function validator(Factory $factory)
{
    $validator = $factory->make($this->all(), $this->rules(), $this->messages(), $this->attributes());

    $validator->after(function($v) {
        if( true ) {
            $v->errors()->add('field', 'Something is wrong with this field!');
        }
    });

    return $validator;
}

The down side is that i need to define how the validator instance is created.

This PR will allow to easily add After Validation Hook to the validator instance without the need to define how the validator instance is made.

class CommentStoreRequest extends FormRequest
{
    protected function validatorHooks($validator)
    {
        $validator->after(function($v) {
            if( true ) {
                $v->errors()->add('field', 'Something is wrong with this field!');
            }
        });
    }
}

@shadoWalker89 shadoWalker89 changed the title Allow to hook into the validator instance from the FormRequest [5.3] Allow to hook into the validator instance from the FormRequest Oct 14, 2016
@taylorotwell
Copy link
Member

taylorotwell commented Oct 15, 2016

You don't actually have to define it in that way... you could just do:

public function validator()
{
     $validator = Validator::make($this->all(), [
          //
     ]);

    // ...

    return $validator;
}

@taylorotwell
Copy link
Member

I might call this method withValidator or something like that... not sure exactly.

@taylorotwell taylorotwell merged commit 87f9761 into laravel:5.3 Oct 15, 2016
@shadoWalker89 shadoWalker89 deleted the after_validation_hook branch December 8, 2016 18:01
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

Successfully merging this pull request may close these issues.

None yet

2 participants