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] Return request data from controller validate() call #19033

Merged
merged 1 commit into from May 3, 2017

Conversation

Projects
None yet
4 participants
@JosephSilber
Contributor

JosephSilber commented May 2, 2017

So that instead of this:

public function store()
{
    $this->validate(request(), [
        'title' => 'required',
        'body' => 'required',
    ]);

    $data = request()->only('title', 'body');

    Post::create($data);
}

You can now do this:

public function store()
{
    $data = $this->validate(request(), [
        'title' => 'required',
        'body' => 'required',
    ]);

    Post::create($data);
}
@taylorotwell

This comment has been minimized.

Member

taylorotwell commented May 3, 2017

How does this behave with deeply nested "dot" notation validation rules?

@JosephSilber

This comment has been minimized.

Contributor

JosephSilber commented May 3, 2017

@taylorotwell do you mean regular dot notation, like this?

$this->validate(request(), [
    'title' => 'required',
    'author.name' => 'required',
    'author.description' => 'required',
]);

This will end up calling only with the keys:

$request->only('title', 'author.name', 'author.description');

Since only returns nested values, this will return the following:

[
    'title' => 'The title',
    'author' => [
        'name' => 'The author\'s name',
        'description' => 'A description of the author',
    ],
]
@morloderex

This comment has been minimized.

Contributor

morloderex commented May 3, 2017

@JosephSilber Addition to @taylorotwell question. What would happen if one these keys should be an uploaded file instance?

@JosephSilber

This comment has been minimized.

Contributor

JosephSilber commented May 3, 2017

@morloderex then you'd get the file as part of the returned array, same as calling $request->only().

*/
public function validateWith($validator, Request $request = null)
{
$request = $request ?: app('request');
$request = $request ?: request();

This comment has been minimized.

@laurencei

laurencei May 3, 2017

Member

Why did this line need to change? I'm trying to understand the difference before/after?

edit: looks like your just switching to the helper, which has the same functionality under the hood? Is that right?

This comment has been minimized.

@JosephSilber
@laurencei

This comment has been minimized.

Member

laurencei commented May 3, 2017

Does this have any effect when using FormRequest?

Could/should the FormRequest also only pass validated variables into the controller after validation? Could be an option to only allow validated data through?

@taylorotwell

This comment has been minimized.

Member

taylorotwell commented May 3, 2017

Seems like if we wanted a validated method could be added to FormRequest that does the same thing.

@taylorotwell

This comment has been minimized.

Member

taylorotwell commented May 3, 2017

Seems fine to me.

@taylorotwell taylorotwell merged commit a58e636 into laravel:master May 3, 2017

2 checks passed

continuous-integration/styleci/pr The StyleCI analysis has passed
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details

@JosephSilber JosephSilber deleted the JosephSilber:validate-return branch May 5, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment