Laravel 5.5
public function register(Request $request){
request()->validate([
'email' => 'required:email'
'password' => 'required|min:6'
]);
return response()->json(["message" => "Hello World"]);
}
如果验证程序失败,则不会给出错误消息。重定向主页
答案 0 :(得分:7)
如果您使用的代码在验证失败时将您重定向到上一页,则表示您没有告诉服务器您希望接收哪种响应。
设置合适的标头以获取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"]);
}
答案 1 :(得分:1)
你可以这样做:
$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"]);
答案 2 :(得分:1)
验证效果很好,但是,$ 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*/
}
}
}
答案 3 :(得分:1)
我在Postman应用程序中测试我的rest api时遇到了同样的问题。
为了修改控制器类文件中的代码,我这样做了,得到了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());
}
答案 4 :(得分:0)
试试这个,希望这段代码可以帮到你
$this->validate($request, [
'email' => 'required|email',
'password' => 'required|min:6'
]);