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"]); 
}

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

你可以这样做:

$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"]);

试试这个,希望这段代码能帮到你

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

验证工作正常,但是 $request->validate() 会将您重定向到上一页。我建议您手动创建验证: Manually Creating Validations。 你可以这样做:

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*/
        }
    }
}

如果您使用的代码在验证失败时将您重定向到上一页,这意味着您没有告诉服务器您希望收到什么样的响应。

设置适当的 header 以获得 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"]);
}

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

  1. 如果我们不想修改我们当前的laravel redirect repose代码,我们必须把Accept:-application/json和ContentType:-application/json

  1. 为了修改控制器 class 文件中的代码,我这样做并得到了 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());
}