如何使用验证 Laravel 验证对象字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49280202/
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
How can I validation object string with validation laravel?
提问by Success Man
I use laravel 5.6
我使用 Laravel 5.6
I use https://laravel.com/docs/5.6/validation#form-request-validationto validation server side
我使用https://laravel.com/docs/5.6/validation#form-request-validation来验证服务器端
My controller like this :
我的控制器是这样的:
<?php
....
use App\Http\Requests\UserUpdateRequest;
class UserController extends Controller
{
...
public function update(UserUpdateRequest $request)
{
// dd($request->all());
}
}
Before run statement in the update method, it will call UserUpdateRequest to validation server side
在更新方法中运行语句之前,它将调用 UserUpdateRequest 到验证服务器端
The validation like this :
像这样的验证:
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class UserUpdateRequest extends FormRequest
{
....
public function rules()
{
dd($this->request->all());
return [
'name' => 'required|max:50',
'gender' => 'required',
'birth_date' => 'required',
'address' => 'required',
'status' => 'required'
];
}
}
The result of dd($this->request->all()); like this :
dd($this->request->all()) 的结果;像这样 :
Array
(
[selected_data] => {"name":"agis","gender":"2","birth_date":"2018-03-13","address":"london"}
)
How can I validation if the data is object array like that?
我如何验证数据是否是这样的对象数组?
回答by Risan Bagja Pradana
You can use a dot notation like so:
您可以像这样使用点符号:
public function rules()
{
return [
'selected_data.name' => 'required|max:50',
'selected_data.gender' => 'required',
'selected_data.birth_date' => 'required',
'selected_data.address' => 'required',
'selected_data.status' => 'required',
];
}
Read more about it here: Validating Array.
在此处阅读更多相关信息:验证数组。
Hope this helps.
希望这可以帮助。