将参数验证为 Laravel 中的 JSON 字符串
Validate Parameters as a JSON String in Laravel
前端部分
参数是这样发送的:
Laravel请求
class CarCreateRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
//TODO: Define authorization logic, possibly a middleware
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'car.name' => 'present|required'
];
}
}
真正的问题
请求 class 始终验证为 false。我检查了 Validating Array 部分,但它看起来像这样发送参数:
car[name]=Spidey Mobile
但是,我需要发送使用 JSON.stringify() 字符串化的数据。
有解决办法吗?看起来点符号不起作用,因为这是 JSON 字符串而不是数组。我试过在评估之前修改请求数据,但我还没有找到任何适用于 Laravel 5.7.
的东西
这是解决方案,我在请求中同时使用了 sanitize 和 validator 方法,以便在评估之前更改请求数据。
class CarCreateRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
//TODO: Define authorization logic, possibly a middleware
return true;
}
public function validator($factory)
{
return $factory->make(
$this->sanitize(), $this->container->call([$this, 'rules']), $this->messages()
);
}
public function sanitize()
{
$this->merge([
'car' => json_decode($this->input('car'), true)
]);
return $this->all();
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'car.name' => 'present|required'
];
}
}
json_decode 会将 JSON 字符串转换为数组,可由 Laravel 验证。
您应该能够像这样在您的请求中覆盖 validationData 方法:
protected function validationData()
{
$this->merge(['car', json_decode($this->car)]); // or what ever your request value is.
return $this->all();
}
前端部分
参数是这样发送的:
Laravel请求
class CarCreateRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
//TODO: Define authorization logic, possibly a middleware
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'car.name' => 'present|required'
];
}
}
真正的问题
请求 class 始终验证为 false。我检查了 Validating Array 部分,但它看起来像这样发送参数:
car[name]=Spidey Mobile
但是,我需要发送使用 JSON.stringify() 字符串化的数据。
有解决办法吗?看起来点符号不起作用,因为这是 JSON 字符串而不是数组。我试过在评估之前修改请求数据,但我还没有找到任何适用于 Laravel 5.7.
的东西这是解决方案,我在请求中同时使用了 sanitize 和 validator 方法,以便在评估之前更改请求数据。
class CarCreateRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
//TODO: Define authorization logic, possibly a middleware
return true;
}
public function validator($factory)
{
return $factory->make(
$this->sanitize(), $this->container->call([$this, 'rules']), $this->messages()
);
}
public function sanitize()
{
$this->merge([
'car' => json_decode($this->input('car'), true)
]);
return $this->all();
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'car.name' => 'present|required'
];
}
}
json_decode 会将 JSON 字符串转换为数组,可由 Laravel 验证。
您应该能够像这样在您的请求中覆盖 validationData 方法:
protected function validationData()
{
$this->merge(['car', json_decode($this->car)]); // or what ever your request value is.
return $this->all();
}