gpt4 book ai didi

laravel - 如何在 Controller Laravel中编写自定义验证规则?

转载 作者:行者123 更新时间:2023-12-04 13:57:29 24 4
gpt4 key购买 nike

我在 Controller Laravel中具有默认验证规则:

$validator = Validator::make($request->all(), [
'email' => 'required|email',
'phone' => 'required|numeric',
'code' => 'required|string|min:3|max:4',
'timezone' => 'required|numeric',
'country' => 'required|integer',
'agreement' => 'accepted'
]);

我试过了,但是不知道如何在函数内部传递一些参数:
 public function boot()
{
Validator::extend('phone_unique', function($attribute, $value, $parameters) {
return substr($value, 0, 3) == '+44';
});
}

如何根据自己的规则扩展此验证?例如,我需要验证输入的隐含性:
$phone = $request->code.' '.$request->phone

检查数据库中是否存在 $phone

我想使用这种方法:
>  $validator->sometimes('phone', 'required|alpha_dash|max:25', function
> ($input) {
> if ((Auth::user()->phone == $input->phone)) {
> return false;
>
> } else {
>
> $t = User::where("phone", $input->phone)->get();
> return ($t->count() > 0) ? false : false;
>
> }
> });

并非在 (True, False)的所有条件下都可以使用。

我添加了新的验证 nickname_unique:
 $validator = Validator::make($request->all(), [
'email' => 'required|email',
'code' => 'required|string|min:3|max:4',
'phone' => 'required|phone_unique',
'timezone' => 'required|numeric',
'country' => 'required|integer',
'nickname' => 'required|alpha_dash|max:25',
'agreement' => 'accepted'
], [
'phone_unique' => 'Phone already exists!',
'nickname_unique' => 'Nickname is busy!',
]);

它不起作用,甚至不能在previos下面调用验证规则:
Validator::extend('nickname_unique', function ($attribute, $value, $parameters, $validator) {

dd("Here");

});

最佳答案

您可以在AppServiceProvider中定义您的自定义验证器,如下所示:

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Validator;

class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
Validator::extend('phone_unique', function ($attribute, $value, $parameters, $validator) {
$inputs = $validator->getData();
$code = $inputs['code'];
$phone = $inputs['phone'];
$concatenated_number = $code . ' ' . $phone;
$except_id = (!empty($parameters)) ? head($parameters) : null;

$query = User::where('phone', $concatenated_number);
if(!empty($except_id)) {
$query->where('id', '<>', $except);
}

return $query->exists();
});

/**
* Register the service provider.
*
* @return void
*/
public function register()
{
//
}
}

You can get all the inputs passed to the validator, by accessing $validator property - getData()



您可以在自定义验证规则之后(仅在 colon之后)向规则数组添加一个额外的参数,如下所示:
'phone' => 'required|phone_unique:1',

Pass the id to be ignored while checking entries into the db



定制验证程序Closure接收四个参数:要验证的 $attribute的名称,属性的 $value,传递给规则的 $parameters数组以及Validator实例。

现在,您可以像这样调用验证器:
$validator = Validator::make($request->all(), [
'email' => 'required|email',
'code' => 'required|string|min:3|max:4',
'phone' => 'required|phone_unique:1',
'timezone' => 'required|numeric',
'country' => 'required|integer',
'agreement' => 'accepted'
], [
'phone_unique' => 'Phone already exists!', // <---- pass a message for your custom validator
]);

查看有关 Custom Validation Rules的更多信息。

关于laravel - 如何在 Controller Laravel中编写自定义验证规则?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41283702/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com