嗨大家好我想注册我的新CustomValidator课程。这包含我为此类创建的手机号码的一些验证
<?php
// This class is located in App\Validator
use Illuminate\Validation\Validator;
class CustomValidator extends Validator {
public function validateIsKyc($attribute, $value, $parameters)
{
return $value == 'is_kyc';
}
}
现在在我的AppServiceProvider中这是我加载该类的代码
<?php
namespace App\Providers;
use Illuminate\Validation\Validator;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
Validator::resolver(function($translator, $data, $rules, $messages)
{
return new CustomValidator($translator, $data, $rules, $messages);
});
}
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
}
根据我的要求,我尝试使用自定义验证,如此
<?php
namespace App\Http\Requests;
use App\Http\Requests\Request;
class CreateRegistrationRequest extends Request
{
/**
* 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 [
'phone_no' => "required|isKyc",
];
}
}
但是我收到了这个错误
FatalErrorException in AppServiceProvider.php line 23: Call to undefined method Illuminate\Validation\Validator::resolver()
请不要知道出了什么问题...
答案 0 :(得分:3)
在文件App\Validator\CustomValidator.php
中,您需要在开头添加
<?php namespace App\Validator;
在文件AppServiceProvider.php
中,您需要在开头添加
use App\Validator\CustomValidator;
并替换此
use Illuminate\Validation\Validator;
用这个
use Validator;
答案 1 :(得分:0)
我的坏家伙,
我已经知道解决方案,我做错了什么,我不知道我会将我的代码放入register函数,而不是boot()函数。
来自
public function register()
{
Validator::resolver(function($translator, $data, $rules, $messages)
{
return new CustomValidator($translator, $data, $rules, $messages);
});
}
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
}
对此,
public function register()
{
}
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
Validator::resolver(function($translator, $data, $rules, $messages)
{
return new CustomValidator($translator, $data, $rules, $messages);
});
}
现在我只能将我的customvalidators管理为一类。 :)
GGWP Laravel!