使用laravel请求类时发生数组错误时调用成员函数失败()
我正在使用一个自定义的请求类进行laravel表单验证 这是我的要求类使用laravel请求类时发生数组错误时调用成员函数失败(),laravel,laravel-form,Laravel,Laravel Form,我正在使用一个自定义的请求类进行laravel表单验证 这是我的要求类 class ContactUsRequest extends FormRequest { /** * Get the validation rules that apply to the request. * * @return array */ public function rules() { return [ 'ln
class ContactUsRequest extends FormRequest
{
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'lname' => 'required'
];
}
/**
* Get the error messages for the defined validation rules.
*
* @return array
*/
public function messages()
{
return [
'lname.required' => 'please enter the last name'
];
}
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
}
这就是我所说的
public function send(ContactUsRequest $request) {
$validator = $request->validated();
if ($validator->fails()) {
return redirect('/contactus')
->withErrors($validator)
->withInput();
} else {
ContactUs::create($request->all());
return redirect('/contactus');
}
}
但当我输入正确的值时,我得到
Symfony\Component\Debug\Exception\FatalThroTableError
(E_错误)对数组上的成员函数的调用失败()
使用表单请求类 如果验证失败,将自动生成重定向响应,以将用户发送回以前的位置。错误也将被闪现到会话中,以便显示。如果请求是AJAX请求,则将向用户返回一个带有422状态码的HTTP响应,其中包括验证错误的JSON表示 为了捕获验证失败,您可以使用验证程序 例如 我们可以像这样保持联系
public function send(ContactUsRequest $request) {
$validator = $request->validated();
ContactUs::create($request->all());
return redirect('/contactus');
}
使用表单请求类 如果验证失败,将自动生成重定向响应,以将用户发送回以前的位置。错误也将被闪现到会话中,以便显示。如果请求是AJAX请求,则将向用户返回一个带有422状态码的HTTP响应,其中包括验证错误的JSON表示 为了捕获验证失败,您可以使用验证程序 例如 我们可以像这样保持联系
public function send(ContactUsRequest $request) {
$validator = $request->validated();
ContactUs::create($request->all());
return redirect('/contactus');
}
这是因为请求对象会自动为您执行此操作,您不需要手动重定向回,
$validator
变量包含已验证的输入
,因此在您的情况下,您不需要执行任何操作,您可以删除if
并安全地重定向
公共功能发送(ContactUsRequest$request){
ContactUs::create($request->validated());
返回重定向('/contactus');
}
}
这是因为请求对象会自动为您执行此操作,您无需手动重定向回,$validator
变量包含已验证的输入
,因此在您的情况下,您无需执行任何操作,您可以删除if
并安全重定向
公共功能发送(ContactUsRequest$request){
ContactUs::create($request->validated());
返回重定向('/contactus');
}
}
谢谢@foued moussi。我添加了一些代码,希望它是正确的:)需要使用ContactUsRequest类也可以:)在注入的cas中ContactUsRequest$request
在您的send
方法中,Validator::make()
在失败的情况下将被忽略。我们不需要这样做,因为这部分将由自定义请求类处理。我是否可以将自定义请求类也添加到代码中,以便我可以接受;)我错误地拒绝了你的建议编辑,你能重做吗?谢谢@foued moussi。我添加了一些代码,希望它是正确的:)需要使用ContactUsRequest类也可以:)在注入的cas中ContactUsRequest$request
在您的send
方法中,Validator::make()
在失败的情况下将被忽略。我们不需要这样做,因为这部分将由自定义请求类处理。我是否可以将自定义请求类也添加到代码中,以便我可以接受;)我错误地拒绝了你的建议,你能重做吗?