我正在尝试在验证后使用passedValidation() 函数在formRequest 中实现强制转换方法。
一切看起来都不错,但是在passedValidation() 中的演员表并没有出现在$request->validated() 中,但是在$request->all() 中是可见的。
我的表格请求
<?php
namespace App\Http\Requests\Models\Company;
use App\Http\Requests\CustomFormRequest;
use Illuminate\Support\Facades\Auth;
use Propaganistas\LaravelPhone\PhoneNumber;
class CompanyFormRequest extends CustomFormRequest
{
protected function prepareForValidation()
{
$this->merge([
'is_presting' => !$this->missing('is_presting')
&& $this->input('is_presting'),
]);
}
public function rules () : array
{
return [
'name' => [ 'required' , 'string' ] ,
'street' => [ 'string' , 'nullable' ] ,
'city' => [ 'string' , 'nullable' ] ,
'post_code' => [ 'string' , 'nullable' ] ,
'TVA' => [ 'string' , 'nullable' ] ,
'language' => [ 'required' , 'string' ] ,
'bank' => [ 'string' , 'nullable' ] ,
'email' => [ 'string' , 'email' , 'nullable' ] ,
'website' => [ 'string' , 'url' , 'nullable' ] ,
'latitude' => [ 'numeric' , 'nullable' ] ,
'longitude' => [ 'numeric' , 'nullable' ] ,
'reference' => [ 'string' , 'nullable' ] ,
'is_presting' => [ 'boolean' ] ,
'phone_country' => [ 'required_with:phone_field' , 'string' ] ,
'phone_field' => [ 'required_with:phone_country' , 'phone:'.$this->input('phone_country') ] ,
];
}
public function authorize () : bool
{
return app () -> runningInConsole ()
|| ( Auth ::check () && Auth ::user () -> can ( 'manage_content' ) );
}
protected function passedValidation ()
{
$this->merge( [
'phone_field' =>
(string) PhoneNumber ::make ( $this -> input ( 'phone_field' ) ,
$this -> input ( 'phone_country' ) )
]);
}
}
$request->all() 验证前
array:16 [▼
"name" => "De Greef SCA"
"street" => "chemin Gérard 527"
"city" => "Tournai"
"post_code" => "6485"
"TVA" => "BE0958232035"
"language" => "fr_FR"
"bank" => "BE96227410211607"
"phone_field" => "0471321102"
"phone_country" => "BE"
"email" => "jpauwels@example.org"
"website" => "http://devos.org/ab-et-itaque-a.html"
"latitude" => -64.420589
"longitude" => 65.667543
"reference" => "OMZ"
"active" => true
"is_presting" => true
]
$request->validated() 验证后
array:16 [▼
"name" => "De Greef SCA"
"street" => "chemin Gérard 527"
"city" => "Tournai"
"post_code" => "6485"
"TVA" => "BE0958232035"
"language" => "fr_FR"
"bank" => "BE96227410211607"
"phone_field" => "0471321102"
"phone_country" => "BE"
"email" => "jpauwels@example.org"
"website" => "http://devos.org/ab-et-itaque-a.html"
"latitude" => -64.420589
"longitude" => 65.667543
"reference" => "OMZ"
"active" => true
"is_presting" => true
]
$request->all() 验证后
array:16 [▼
"name" => "De Greef SCA"
"street" => "chemin Gérard 527"
"city" => "Tournai"
"post_code" => "6485"
"TVA" => "BE0958232035"
"language" => "fr_FR"
"bank" => "BE96227410211607"
"phone_field" => "+32471321102"
"phone_country" => "BE"
"email" => "jpauwels@example.org"
"website" => "http://devos.org/ab-et-itaque-a.html"
"latitude" => -64.420589
"longitude" => 65.667543
"reference" => "OMZ"
"active" => true
"is_presting" => true
]
我们在 $request->all() 中看到验证后在 'phone_field' 上很好地执行了强制转换,但为什么它没有出现在 $request->validated() 中?
谢谢。