我只想要通过验证的数据。并且不希望使用laravel-validation
进行任何错误或重定向。到目前为止,我已经能够使用laravel-request
停止重定向,但如果我将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
,但它也没有按预期工作:
内部控制器
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中的其他验证函数,当我的字段增加时,我不认为写条件是否有助于我。
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();
}
}
您可以执行任何您想要的任务,而无需重定向,而无需重定向。