我有一个小问题。我使用Laravel创建简单的API。当我使用验证时,如果它失败了,我得到了一个共同的信息:
{
"result": false,
"message": "The given data failed to pass validation.",
"details": []
}
但是我如何才能获得有关哪个字段失败的详细信息以及为什么会这样:
{
"result":false,
"message":"The given data failed to pass validation.",
"details":{
"email":[
"The email field is required."
],
"password":[
"The password must be at least 3 characters."
]
}
}
我在控制器中的代码如下所示:
protected function validator(array $data)
{
$validator = Validator::make($data, [
'name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users',
'password' => 'required|string|min:3',
]);
return $validator;
}
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
'role_id' => 2
]);
}
答案 0 :(得分:1)
我在项目中使用了验证:
1。我创建了app / http / requests / CreateUserRequestForm.php
public function rules()
{
return [
"name" => 'required',
"address" => 'required',
"phnumber" => 'required|numeric',
];
}
public function messages()
{
return [
'name.required' => 'Please Enter Name',
'addresss.required' => 'Please Enter Address',
'phnumber.required' => 'Please Enter PhNumber'
];
}
使用App \ Http \ Requests \ CreateUserRequestForm;
public function createUser(CreateUserRequestForm $request)
{
// create
$user= UserModel::create([
'name' => $request->input('name'),
'address' => $request->input('address'),
'phnumber' => $request->input('phnumber')
]);
return response()->json(['User' => $user]);
}
答案 1 :(得分:1)
您应该确保要发送带有Accept:application / json标头的请求。
否则,Laravel不会检测到它是一个API请求,
如果验证失败,将生成重定向响应以将用户发送回其先前的位置。错误还将闪现到会话中,以便显示。如果该请求是AJAX请求,则将向用户返回带有422状态代码的HTTP响应,其中包括验证错误的JSON表示形式。
答案 2 :(得分:0)
尝试这个我没试过,但它应该适合你。
您可以使用withValidator方法。这种方法完全接收 构造的验证器,允许您以前调用它的任何方法 实际上评估了验证规则。
从这里参考。 laravel validation
/**
* Configure the validator instance.
*
* @param \Illuminate\Validation\Validator $validator
* @return void
*/
public function withValidator($validator)
{
$validator->after(function ($validator) {
if ($this->somethingElseIsInvalid()) {
$validator->errors()->add('email', 'Please enter valid email id');
}
});
}
答案 3 :(得分:0)
最好在同一个过程中处理验证器,如下所示:
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|confirmed',
]);
if($validator->fails()){
return response()->json([
"error" => 'validation_error',
"message" => $validator->errors(),
], 422);
}
$request->merge(['password' => Hash::make($request->password)]);
try{
$user = User::create($request->all());
return response()->json(['status','registered successfully'],200);
}
catch(Exception $e){
return response()->json([
"error" => "could_not_register",
"message" => "Unable to register user"
], 400);
}
}
答案 4 :(得分:0)
试试这个:
public function create(){
// ------ Validate -----
$this->vallidate($request,[
'enter code here`name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users',
'password' => 'required|string|min:3'
]);
// ------ Create user -----
$user = User::create(['name' => $request->name']);
return response()->json([
'message' => "success",
'data' => $user``
]);
}