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.5] Add a validate method onto the request #19063

Merged
merged 1 commit into from
May 5, 2017

Conversation

JosephSilber
Copy link
Member

@JosephSilber JosephSilber commented May 5, 2017

This will allow calling validate directly on the request object:

$data = request()->validate([
    'title' => 'required',
    'body' => 'required',
]);

@JosephSilber JosephSilber changed the title [5.5] Request validate [5.5] Add a validate method onto the request May 5, 2017
@tillkruss
Copy link
Collaborator

tillkruss commented May 5, 2017

A request shouldn't know how to validate itself!!!11!!one!!!!!111!!eleven!!111!!elf!!

@ntzm
Copy link
Contributor

ntzm commented May 5, 2017

I don't understand the point of this

@reinink
Copy link
Contributor

reinink commented May 5, 2017

I'm very much in favour of this. I've been doing this with macros for quite some time:

public function registerValidate()
{
    Request::macro('validate', function ($rules, $messages = [], $customAttributes = []) {
        (new class() {
            use ValidatesRequests;
        })->validate($this, $rules, $messages, $customAttributes);
    });
}

Which, as you can see, it takes a little magic when done as a macro, so having this built in gets a big 👍 fro me.

@reinink
Copy link
Contributor

reinink commented May 5, 2017

Hmm, I guess I could have use the validator() trick. Interesting.

@taylorotwell taylorotwell merged commit 5b7d037 into laravel:master May 5, 2017
@JosephSilber JosephSilber deleted the request-validate branch May 7, 2017 04:16
@gocanto
Copy link
Contributor

gocanto commented Aug 4, 2017

Why didn't this go to the request object? :)

@JosephSilber
Copy link
Member Author

@gocanto Laravel's components are decoupled from each other as much as possible. You should be able to pull in only some of its components into a non-Laravel project, and have it work without pulling in all of the framework.

Since the Request class lives in the Http component, it doesn't have access to the validator, which lives in the Validation component.

In addition to all of the standalone components, Laravel has a separate Foundation component that serves as the glue between all other components for when you're using the full laravel framework. This Foundation component is tightly coupled to all of the other components, so it has access to both the request class and the validator. This makes it the perfect place to hook up validation with the request 👌

@gocanto
Copy link
Contributor

gocanto commented Aug 4, 2017

@JosephSilber Excelente. Thanks for the explanation!

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

6 participants