php - 如何排除所有未通过验证 laravel 的数据?
问题描述
我只想要通过验证的数据。并且不希望使用laravel-validation
. 到目前为止,我已经能够使用 a 停止重定向,laravel-request
但如果我将方法保留为 not any validation
,似乎无法正常工作。failedValidation
throw
exception
我user
有谁可以填充行或将它们留空的数据数组。如果他们将必填字段连续留空,那么我想将它们从validation
.
ExpenditureExtry.php
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Http\Exceptions\HttpResponseException;
class ExpenditureEntry extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'date' => 'required',
'description.*' => 'nullable',
'account_head.*' => 'required',
'amount.*' => 'required_with:account_head.*'
];
}
/**
* Failed validation disable redirect
*
* @param Validator $validator
*/
protected function failedValidation(Validator $validator)
{
//throw new HttpResponseException(response()->json($validator->errors(), 422));
}
}
控制器内部
public function store(ExpenditureEntry $req)
{
return $req->validated();
}
我希望它只返回那些通过验证的数据。目前它返回所有数据。
预期产出
// http://127.0.0.1:8000/expenditure/add
{
"date": "03 Sep 2018",
"description": {
"1": null,
},
"account_head": {
"1": "5",
},
"amount": {
"1": "5000",
}
}
实际输出
// http://127.0.0.1:8000/expenditure/add
{
"date": "03 Sep 2018",
"description": {
"0": null,
"1": null,
"2": null,
"3": "sas asd adas",
"null": null
},
"account_head": {
"0": "3",
"1": "5",
"2": null,
"3": null,
"null": null
},
"amount": {
"0": null,
"1": "5000",
"2": "5000",
"3": null,
"null": null
}
}
我也尝试Validator::make
在控制器中使用 a ,但它也没有按预期工作:
内部控制器
public function store(Request $req)
{
$validator = Validator::make($req->except(['account_head.null', 'description.null', 'amount.null']), [
'date' => 'required',
'account_head.*' => 'required',
'description.*' => 'nullable',
'amount.*' => 'required_with:account_head.*'
]);
return $validator->valid();
}
输出
// http://127.0.0.1:8000/expenditure/add
{
"_token": "EmiRpnXhPHUPl3HIzuYLrtuLaGj9Ruv8AySe11Bp",
"date": "03 Sep 2018",
"description": [
null,
null,
null,
"sas asd adas"
]
}
PS:laravel
如果可能的话,我想要一个基于的解决方案。如果没有这样的解决方案是可能的,我实际上有一个解决方案来手动验证它们。
我的解决方案 遍历所有数据并手动验证它们。我对这种方法的问题是现在我不能使用 laravel 中的其他验证函数,当我的字段增加时,我认为编写 if 条件对我没有帮助。
public function store(Request $req)
{
$datas = $req->except(['_token', 'account_head.null', 'description.null', 'amount.null']);
$countR = count($datas['account_head']);
$validated = [];
$cv = 0;
for ($i=0; $i < $countR; $i++) {
if($datas['account_head'][$i] == null) continue;
else if($datas['amount'][$i] == null) continue;
$validated['account_head'][$cv] = $datas['account_head'][$i];
$validated['description'][$cv] = $datas['description'][$i];
$validated['amount'][$cv] = $datas['amount'][$i];
$cv++;
}
return $validated;
}
输出
// http://127.0.0.1:8000/expenditure/add
{
"account_head": [
"5"
],
"description": [
null
],
"amount": [
"5000"
]
}
解决方案
据我了解您的问题,以下代码将解决您的问题。
public function store(Request $req)
{
$validator = Validator::make($req->except(['account_head.null', 'description.null', 'amount.null']), [
'date' => 'required',
'account_head.*' => 'required',
'description.*' => 'nullable',
'amount.*' => 'required_with:account_head.*'
]);
if ($validator->fails()) {
// Do Whatever you wants here.
// return redirect()->back()->withErrors($validator)->withInput();
}
}
你可以做任何你想做的任务,而不用重定向错误,而不需要任何重定向。
推荐阅读
- javascript - 如何将 vscode css 颜色选择器功能带入带有匹配颜色名称格式的字符串的 js 文件中?
- c# - 如何通过查询连接 2 个 SQL Server 表并动态使用数据
- r - R中按组替换缺失值
- asp.net - 允许操作结果接受抽象类作为参数的任何解决方法
- c# - 从亚马逊 MWS XML 产品提要中获取变体
- puppet - rspec-puppet:找不到所需的类
- python - 使用 pandas groupby 的 csv 列问题
- amazon-redshift - Amazon Redshift 术语
- scala - 如何将 foreachPartition 中的函数结果发送到驱动程序节点?
- quaternions - 从不带磁力计的 6 轴 IMU 计算方向