繁体   English   中英

Laravel验证程序无法正常工作-重定向主页

[英]Laravel Validator Not Working Properly - Redirecting main page

Laravel 5.5

public function register(Request $request){


    request()->validate([
        'email' => 'required:email'
        'password' => 'required|min:6'
    ]);

    return response()->json(["message" => "Hello World"]);

}

如果验证器失败,则不给出错误消息。 重定向主页

如果验证失败时,您使用的代码将您重定向到上一页,则意味着您没有告诉服务器您想要接收哪种响应。

设置适当的标头以获取JSON。 它将使验证程序发送JSON作为响应。 例如:

$.ajax({
  headers: {
    Accept : "application/json"
  },
  ...
});

然后此代码将按预期工作:

public function register(Request $request) 
{
    $request->validate([
        'email' => 'required:email'
        'password' => 'required|min:6'
    ]);

    return response()->json(["message" => "Hello World"]);
}

您可以这样做:

$validator = Validator::make($request->all(), [
    'email' => 'required|email', //use pipe here to apply multiple validations rules and add a ','
    'password' => 'required|min:6'
]);

if ($validator->fails()) {
    return response()->json(['errors' => $validator->errors()]);
}
return response()->json(["message" => "Hello World"]);

验证运行良好,但是$ request-> validate()会将您重定向到上一页。 我建议您手动创建验证: 手动创建验证 您可以执行以下操作:

use Illuminate\Http\Request;
use Validator;
class YourClass extends Controller{
    public function yourFunction(Request $request){
        $validator = Validator::make($request->all(),[
            'field_1' => 'rule1|rule2',
            'field_2' => 'rule1|rule2'
        ]);

        if($validator->fails()){
            return response()->json($validator->errors());
        }else{
            /*Something else*/
        }
    }
}

在Postman应用程序中测试我的其余api时,我遇到了同样的问题。

  1. 如果我们不想修改当前的laravel重定向休假代码,则必须放入Accept:-application / json和ContentType:-application / json

在此处输入图片说明

  1. 为了修改控制器类文件中的代码,我做到了这一点,并得到了json响应,而不是重定向到主页。

      public function register(Request $request) { $validator = Validator::make($request->all(), [ 'name' => 'required|string|max:255', 'email' => 'required|string|email|max:255|unique:users', 'password' => 'required|string|min:6', ]); if ($validator->fails()) { return response()->json($validator->errors()); }else{ //do something } } 

在它看起来像下面的代码之前,它被重定向到主页

这是验证器功能

protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => 'required|string|max:255',
            'email' => 'required|string|email|max:255|unique:users',
            'password' => 'required|string|min:6',
        ]);
    }
public function register(Request $request)
{

    // Here the request is validated. The validator method is located
    // inside the RegisterController, and makes sure the name, email
    // password and password_confirmation fields are required.
    $this->validator($request->all())->validate();

    // A Registered event is created and will trigger any relevant
    // observers, such as sending a confirmation email or any 
    // code that needs to be run as soon as the user is created.
    event(new Registered($user = $this->create($request->all())));

    // After the user is created, he's logged in.
    $this->guard()->login($user);

    // And finally this is the hook that we want. If there is no
    // registered() method or it returns null, redirect him to
    // some other URL. In our case, we just need to implement
    // that method to return the correct response.
    return $this->registered($request, $user)
                    ?: redirect($this->redirectPath());
}

试试看,希望这段代码可以为您提供帮助

$this->validate($request, [
        'email'    => 'required|email',
        'password' => 'required|min:6'
    ]);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM