andersao / andersao/l5-repository

How to use the validator correctly

未關閉
#562 1 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視
主要語言
PHP
星號
4.2k
分支
880
PR 合併指標
30 天內沒有已合併 PR

描述

Hello! How to use the validator correctly.
Generally we will use [Form Request Validation](https://laravel.com/docs/5.7/validation#form-request-validation) . but I see Validator Class .

```
public function store(ArticleCreateRequest $request)
{

try {

$this->validator->with($request->all())->passesOrFail(ValidatorInterface::RULE_CREATE);

$article = $this->repository->create($request->all());

$response = [
'message' => 'Article created.',
'data' => $article->toArray(),
];

if ($request->wantsJson()) {

return response()->json($response);
}

return redirect()->back()->with('message', $response['message']);
} catch (ValidatorException $e) {
if ($request->wantsJson()) {
return response()->json([
'error' => true,
'message' => $e->getMessageBag()
]);
}

return redirect()->back()->withErrors($e->getMessageBag())->withInput();
}
}

```

```
class ArticleValidator extends LaravelValidator
{
/**
* Validation Rules
*
* @var array
*/
protected $rules = [
ValidatorInterface::RULE_CREATE => [],
ValidatorInterface::RULE_UPDATE => [],
];
}
```

Using a Validator Class. `ArticleValidator`. This file `ArticleCreateRequest`.what is it used for?
Thank you!

貢獻指南

開啟貢獻指南

評估

這個 Issue 還沒有評估資料。

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。