dpict99695329
2017-12-18 13:09
采纳率: 100%
浏览 85
已采纳

Laravel自定义验证和ajax

The validation on my Laravel 4.2 project was done with Ardent package. After going to Laravel 5.5 I have eliminated Ardent and wanted to do Laravel's native validation with form requests.

The problem I have is that the Ajax call was validated before like this:

public function postRegisterAjax(A)
{
    try {
         ...
    } catch (ExceptionBag $e) {
        $msg = $e->getMessageBag()->all(':message');
        $status = Status::ERROR;
    }

    return $this->responseJson($status, $msg);
}

Now I introduced UserValidationRequest class and I would like Ajax call to throw me an error message without the need to reload the page. In order to do that, I need to forward status and message as Json response.

I somehow tried to do that with after validation hooks, but it doesn't work:

protected function getValidatorInstance()
{
    $validator = parent::getValidatorInstance();

    if ($validator->fails()) {
        \Log::info($validator->errors());

        $msg = $validator->errors();
        $status = Status::ERROR;

        return response()->json(['response' => [
            'status' => $status,
            'msg' => $msg,
        ]]);
    }

    return $validator;
}

The code fails on return response() saying that Method passes does not exist (Illuminate/Support/Traits/Macroable.php:96).

Does anyone know what seems to be the issue?

图片转代码服务由CSDN问答提供 功能建议

我的Laravel 4.2项目的验证是使用Ardent包完成的。 在去Laravel 5.5后,我已经取消了Ardent,并希望通过表单请求进行Laravel的本机验证。

我遇到的问题是之前验证了Ajax调用,如下所示:

  public function postRegisterAjax(A)
 {\  n尝试{
 ... 
} catch(ExceptionBag $ e){
 $ msg = $ e-> getMessageBag() - > all(':message'); 
 $ status = Status :: 错误; 
} 
 
返回$ this-> responseJson($ status,$ msg); 
} 
   
 
 

现在我介绍 UserValidationRequest 类,我希望Ajax调用给我一个错误信息而不需要重新加载页面。 为了做到这一点,我需要将状态和消息转发为Json响应。

我试图在验证挂钩之后尝试这样做,但它不起作用:

  protected function getValidatorInstance()
  {
 $ validator = parent :: getValidatorInstance(); 
 
 if if($ validator-> failed()){
 \ Log :: info($ validator-> errors()); 
 \  n $ msg = $ validator-> errors(); 
 $ status = Status :: ERROR; 
 
返回response() - > json(['response'=> [
'status'=  > $ status,
'msg'=> $ msg,
]]); 
} 
 
返回$ validator; 
} 
   
 
  

代码在 return response()上失败,说方法传递不存在 Illuminate / Support / Traits / Macroable.php:96 )。

有谁知道这个问题究竟是什么?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duanjiwei1283 2017-12-20 08:25
    已采纳

    From Laravel 5.x version (not sure), failedValidation() method was introduced in form requests instead of what Laravel 4.x had as response().

    I resolved my issue by tailoring the response to my needs by overriding that method in my form request:

    public function failedValidation(Validator $validator)
    {
        if ($validator->fails()) {
            $status = Status::ERROR;
            throw new HttpResponseException(response()->json(["response" => [
                'msg'    => $validator->errors()->all(':message'),
                'status' => $status
            ]]));
        }
    
        return response()->json(["response" => [
            'msg'    => 'User successfully registered',
            'status' => Status::SUCCESS
        ]]);
    }
    
    打赏 评论

相关推荐 更多相似问题