如何仅从 laravel FormRequest 获取经过验证的数据?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47936337/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 17:08:49  来源:igfitidea点击:

How do I get ONLY the validated data from a laravel FormRequest?

phplaravelvalidationrequest-validation

提问by JonTroncoso

Lets say I have the following Custom Request:

假设我有以下自定义请求:

class PlanRequest extends FormRequest
{
    // ...


    public function rules()
    {

        return
        [
            'name'              => 'required|string|min:3|max:191',
            'monthly_fee'       => 'required|numeric|min:0',
            'transaction_fee'   => 'required|numeric|min:0',
            'processing_fee'    => 'required|numeric|min:0|max:100',
            'annual_fee'        => 'required|numeric|min:0',
            'setup_fee'         => 'required|numeric|min:0',
            'organization_id'   => 'exists:organizations,id',
        ];
    }
}

When I access it from the controller, if I do $request->all(), it gives me ALLthe data, including extra garbage data that isn't meant to be passed.

当我从控制器访问它时,如果我这样做$request->all(),它会给我所有数据,包括不打算传递的额外垃圾数据。

public function store(PlanRequest $request)
{
    dd($request->all());
    // This returns
    [
        'name'              => 'value',
        'monthly_fee'       => '1.23',
        'transaction_fee'   => '1.23',
        'processing_fee'    => '1.23',
        'annual_fee'        => '1.23',
        'setup_fee'         => '1.23',
        'organization_id'   => null,
        'foo'               => 'bar', // This is not supposed to show up
    ];
}

How do I get ONLYthe validated data without manually doing $request->only('name','monthly_fee', etc...)?

如何在不手动执行的情况下获取经过验证的数据$request->only('name','monthly_fee', etc...)

回答by Sapnesh Naik

$request->validated()will return only the validated data.

$request->validated()将只返回经过验证的数据。

Example:

例子:

public function store(Request $request)
{
    $request->validate([
        'title' => 'required|unique:posts|max:255',
        'body' => 'required',
    ]);

    $validatedData = $request->validated();

}


Alternate Solution:

替代解决方案:

$request->validate([rules...])returns the only validated data if the validation passes.

$request->validate([rules...])如果验证通过,则返回唯一验证的数据。

Example:

例子:

public function store(Request $request)
{

    $validatedData = $request->validate([
        'title' => 'required|unique:posts|max:255',
        'body' => 'required',
    ]);

}

回答by JonTroncoso

OK... After I spent the time to type this question out, I figured I'd check the laravel "API" documentation: https://laravel.com/api/5.5/Illuminate/Foundation/Http/FormRequest.html

好的......在我花时间输入这个问题后,我想我会检查laravel“API”文档:https://laravel.com/api/5.5/Illuminate/Foundation/Http/FormRequest.html

Looks like I can use $request->validated(). Wish they would say this in the Validation documentation. It makes my controller actions look pretty slick:

看起来我可以使用$request->validated(). 希望他们能在验证文档中说明这一点。它使我的控制器操作看起来非常流畅:

public function store(PlanRequest $request)
{
    return response()->json(['plan' => Plan::create($request->validated())]);
}