简体   繁体   English

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

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

Laravel 5.5 Laravel 5.5

public function register(Request $request){


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

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

}

If validator is fails, not giving error messages. 如果验证器失败,则不给出错误消息。 Redirecting main page 重定向主页

If the code you're using redirects you to the previous page when validation fails, it means that you didn't tell the server what kind of response you want to receive. 如果验证失败时,您使用的代码将您重定向到上一页,则意味着您没有告诉服务器您想要接收哪种响应。

Set a proper header to get JSON. 设置适当的标头以获取JSON。 It will make the validator send JSON in response. 它将使验证程序发送JSON作为响应。 For example: 例如:

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

Then this code will work as expected: 然后此代码将按预期工作:

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

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

You can do this like this : 您可以这样做:

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

The validation is working well, but, $request->validate() will redirect you to the previous page. 验证运行良好,但是$ request-> validate()会将您重定向到上一页。 I recommend you to manually create your validations: Manually Creating Validations . 我建议您手动创建验证: 手动创建验证 You could do something like this: 您可以执行以下操作:

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

i had the same problem when testing my rest api in Postman application. 在Postman应用程序中测试我的其余api时,我遇到了同样的问题。

  1. if we don't want to modify our current code of laravel redirect repose, we have to put Accept:-application/json and ContentType:-application/json 如果我们不想修改当前的laravel重定向休假代码,则必须放入Accept:-application / json和ContentType:-application / json

在此处输入图片说明

  1. For modifying code in controller class file, i did it like this and got the json response instead of redirecting to home page. 为了修改控制器类文件中的代码,我做到了这一点,并得到了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 } } 

before it looks like below codes it was redirecting to home page 在它看起来像下面的代码之前,它被重定向到主页

This is validator function 这是验证器功能

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());
}

try this, hope this code can help you 试试看,希望这段代码可以为您提供帮助

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

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

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