我在控制器中进行了自定义验证,以验证电话号码,
控制器
$this->validate($request, [
...
'phonenumber' => 'required|phone_number'
]);
AppServiceProvider
public function boot(){
$this->app['validator']->extend('is_phone', function($attribute, $value, $parameters) {
...
if($rest_result->status == 200){
// I need this $rest_result->phone_number ;
return true ;
}
return false ;
});
}
我实现了验证程序,并将其放在boot
的{{1}}方法中,以便我可以在其他地方重复使用它。我使用Web服务来验证号码,如果呼叫成功,服务会以我在应用中需要的相应国家格式格式化号码。
如何将该值传递回控制器?
奖金:如果它还可以覆盖AppServiceProvider
中的值以便表单获得它也会非常酷
答案 0 :(得分:1)
我找到了一种方法来一次性完成这一切。
完成验证后,在请求上使用merge
方法将已验证和已修改的输入合并回请求对象。
你会把以下
public function boot(){
$this->app['validator']->extend('is_phone', function($attribute, $value, $parameters) {
...
if($rest_result->status == 200){
request()->merge(['phone' => $rest_result->phone]);
return true;
}
return false ;
});
}
现在在您的控制器中,该电话将包含在请求中...显然,在您实际制作验证对象之前,电话号码不会被服务返回的电话号码替换。
$phone = request()->phone;