如何排除所有未通过验证的数据laravel?

How to exclude all the data that fail validation laravel?

我只想要通过验证的数据。并且不希望使用 laravel-validation 出现任何错误或重定向。到目前为止,我已经能够使用 laravel-request 停止重定向,但是如果我将 failedValidation 方法保留为不 throw 任何 validation 似乎无法正常工作 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();
}

我希望 return 只有那些通过验证的数据。目前它return所有数据。

预期输出

// 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 中的其他验证函数,并且当我的字段增加时,我认为编写 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();
  }
}

你可以做任何你想做的事情,而不需要任何重定向。