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
Member

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

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

@JosephSilber
Copy link
Member Author

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 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
Member Author

@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();
Copy link
Contributor

@laurencei laurencei May 3, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@laurencei correct.

@laurencei
Copy link
Contributor

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

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

@taylorotwell
Copy link
Member

Seems fine to me.

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.

4 participants