Php Laravel:FormRequest不会只返回已验证的数据
我用一些validationl得到了object,request-validated方法甚至返回不在验证规则中的字段。下面是我的验证类的外观Php Laravel:FormRequest不会只返回已验证的数据,php,laravel,validation,Php,Laravel,Validation,我用一些validationl得到了object,request-validated方法甚至返回不在验证规则中的字段。下面是我的验证类的外观 /** * Get the validation rules that apply to the request. * * @return array */ public function rules() { return [ 'terminal' => ['required'], 'terminal.
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'terminal' => ['required'],
'terminal.followings' => ['boolean']
];
}
{
"client" : {
"pk" : "2128436070",
"username" : "miljan_rakita",
"daily_following" : 1000
},
"terminal" : {
"followings" : true,
"followingasdad" : "asdasdas",
"followers" : false,
"exact_account" : true
}
}
这就是我的json请求体的样子
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'terminal' => ['required'],
'terminal.followings' => ['boolean']
];
}
{
"client" : {
"pk" : "2128436070",
"username" : "miljan_rakita",
"daily_following" : 1000
},
"terminal" : {
"followings" : true,
"followingasdad" : "asdasdas",
"followers" : false,
"exact_account" : true
}
}
通过此验证,我希望只得到以下对象:
{
"terminal" : {
"followings" : true,
}
}
但当我添加验证数据时:
public function store(Test $request)
{
dd($request->validated());
}
响应是完整的终端对象,甚至是未验证的项
array:1 [▼
"terminal" => array:4 [▼
"followings" => true
"followingasdad" => "asdasdas"
"followers" => false
"exact_account" => true
]
]
因此,我希望通过它只得到具有以下字段的终端对象。但我得到了终端对象中的所有其他字段
我只能从数组中提取所需的项,但我想知道为什么我不能只获取已验证的项。表单验证只检查传入请求是否至少包含所有必需的值,因此您收到的请求数据与预期一致。话虽如此,您希望调整传入数据,这可以在最新版本中完成。您可以通过在formrequest中实现
passedValidation()
方法来实现这一点(Test
)
array:1 [▼
"terminal" => array:4 [▼
"followings" => true
"followingasdad" => "asdasdas"
"followers" => false
"exact_account" => true
]
]
尝试添加
protected$filleble=['terminal.followings']代码>在您的模型中,它与模型无关,这与请求和验证有关@Josephoh我记得现在你需要在store方法中传递你的规则类,它会给你验证数组的新实例