[英]While using request validation class through Laravgel API, Error Call to a member function validated() on null
I am using Laravel Request validation class to validate input through API Reference我正在使用 Laravel 请求验证 class 来验证通过 API 参考的输入
I implemented as it is mentioned in documentation and got the following error:我按照文档中提到的方式实现并收到以下错误:
Symfony\Component\Debug\Exception\FatalThrowableError: Call to a member function validated() on null Symfony\Component\Debug\Exception\FatalThrowableError:在 null 上调用成员 function 验证()
The only difference I feel that they submit a form from a web and I am making a request from postman.我觉得唯一的区别是他们从 web 提交表格,而我正在向 postman 提出请求。
It's been a couple of hours, no clue yet.已经几个小时了,还没有任何线索。 any help, please?有什么帮助吗?
use Illuminate\Foundation\Http\FormRequest;
class UserTypeRequest extends FormRequest
{
public function authorize()
{
return true;
}
public function rules()
{
return [
'user_type' => 'required',
];
}
}
Calling validation in controller:在 controller 中调用验证:
$this->user_type_request = new UserTypeRequest();
$this->user_type_request->validated();
It should be validation input filed coming from the api, instead its showing null in lavatorial它应该是来自 api 的验证输入文件,而不是在厕所中显示 null
Try and pass the form request via Dependancy injection:尝试通过依赖注入传递表单请求:
public function store(UserTypeRequest $request)
{
$var = $request->validated();
...
}
Creating a new form request probably is not validating the previous request (or getting the validation response from the previous request).创建一个新的表单请求可能不会验证前一个请求(或从前一个请求中获取验证响应)。
You don't need to call the validate
function if you're passing the Form Request class in the controller function, just proceed and your request is automatically validated如果您在 controller function 中传递表单请求 class,则无需调用validate
函数,只需继续并自动验证您的请求
public function controllerMethod(\App\Http\Requests\UserTypeRequest $request) // Just pass it here
{
// Data is already checked
$data = $request->validated();
}
Hope this helps希望这可以帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.