Skip to content

Conversation

@JosephSilber
Copy link
Contributor

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