Laravel 5.6 $this->validate vs Validator::make()



我看到还有其他一些问题,他们问$this->validateValidator::make()有什么区别。不过,他们并没有真正回答我想知道的概念问题。

这些都有适当的用途吗?比如什么时候使用一个而不是另一个?

我目前如何使用它在我的 API 类中,我使用 if else 和$validator::make()(如下所示(,而在程序的 Web 部分中,我使用 $this->validate()(也在下面(

这是使用它的正确方法吗?

$validator::制作:

public function store(Request $request)
{
$validator = Validator::make($request->all(),[
'name' => 'required',
'url' => 'required',
'isPublic' => 'required'
]);
if($validator->fails()){
return response($validator->messages(), 200);
} else {
Helpers::storeServer($request);
return response()->json([
'message'=> ['Server Stored']
]);
}
}

$this->验证:

public function store(Request $request)
{
$this->validate($request, [
'name' => 'required',
'url' => 'required',
'isPublic' => 'required'
]);
Helpers::storeServer($request);
return redirect('dashboard')->with('success', 'Server stored');
}

不,它们以两种不同的方式做同样的事情。我的意思是,从字面上看,$this->validate()在验证类上调用make()方法。如果您查看由控制器实现的 ValidatesRequests.php.php您的控制器扩展了它。

validate()方法调用:

$validator = $this->getValidationFactory()
->make($request->all(), $rules, $messages, $customAttributes);

因此,它最终会使用make()方法。它的处理方式有所不同,因为$this->validate()调用:

if ($validator->fails()) {
$this->throwValidationException($request, $validator);
}

因此,使用Validator::make()将允许您自己处理异常,而不是$this->validate()自动为您引发验证异常。这对于在重定向之前执行某些操作很有用。您在第一个示例中显示了这一点,因为在决定如何处理验证之前检查验证是否失败。 在第二个示例中,您知道如果验证失败,它将自动拒绝请求...

如果$this->validate上的任何规则失败,将自动抛出错误

$validator::make:您可以轻松处理错误,并且可能希望将任何数据包装到 array(( 并传递给$validator::make:进行验证

以及如果您想使用 FormRequest + 路由模型绑定

你的商店((看起来像这样

public function store(YourFormRequestRules $request)
{
Helpers::storeServer($request);
return redirect('dashboard')->with('success', 'Server stored');
}
public function update(YourFormRequestRules $request, Post $post)
{
$post->title = $request->input('title');
$post->save();
return redirect()->route('example.index');
}

就这样

使用$this->validate()YourController扩展了一个使用ValidatesRequeststrait 的Controller类,它看起来像这样:

namespace AppHttpControllers;
use IlluminateFoundationBusDispatchesJobs;
use IlluminateRoutingController as BaseController;
use IlluminateFoundationValidationValidatesRequests;
use IlluminateFoundationAuthAccessAuthorizesRequests;
class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
}

ValidatesRequests特征本身由基本控制器获取的几种方法组成,然后是您自己的控制器,其中包括:

validateWith($validator, Request $request = null)
...
validate(Request $request, array $rules, array $messages = [], array $customAttributes = [])
...
validateWithBag($errorBag, Request $request, array $rules, array $messages = [], array $customAttributes = [])
... and so on...

这些方法有助于使一些验证和请求错误处理非常方便,实际上考虑了提到的validate()方法。

当传递的请求中存在验证错误时,它可以帮助您处理响应,而无需使用不需要的逻辑使控制器字面化;即当它是 ajax 调用时,它会返回带有 json 正文的 422 响应,同时返回填充错误包并填充$errors变量以在非 ajax 的刀片模板中使用。

总之,这只会帮助您不想通过创建 Validator 实例进行手动验证,因此让开发人员专注于执行实际工作;)

更新:

$validator = Validator::make() //creates an instance of the validator for further operations
// You can even do after the creating the instance of validator:
$this->validateWith($validator, $request)
//Lets do both steps above i.e the validation and trigger the Validation Exception if there's failure.
$this->validate($request, [...rules...], [...messages..]) 

检查:https://laravel.com/docs/5.6/validation#quick-writing-the-validation-logic

验证器助手让我更开心

$validator = validator()->make(request()->all(), [
'type' => 'required|integer'
]);
if ($validator->fails())
{
redirect()->back()->with('error', ['your message here']);
}

拉拉维尔给帮助者,使发展更有说服力。

最新更新