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

Merged
merged 1 commit into from May 3, 2017

Conversation

@JosephSilber
Copy link
Contributor

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

@taylorotwell taylorotwell commented May 3, 2017

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

@JosephSilber
Copy link
Contributor Author

@JosephSilber 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
Copy link
Contributor

@morloderex morloderex commented May 3, 2017

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

@JosephSilber
Copy link
Contributor Author

@JosephSilber 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
Contributor

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

JosephSilber May 5, 2017
Author Contributor

@laurencei correct.

@laurencei
Copy link
Contributor

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

@taylorotwell taylorotwell commented May 3, 2017

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

@taylorotwell
Copy link
Member

@taylorotwell taylorotwell commented May 3, 2017

Seems fine to me.

@taylorotwell taylorotwell merged commit a58e636 into laravel:master May 3, 2017
2 checks passed
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
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

4 participants