如何在 laravel 5.8 中显示自定义消息?
How to show custom messages in laravel 5.8?
我正在使用 laravel 5.8 版本,我有一个 api 负责注册,我创建了一个请求文件,其中包含 rules() 和 messages() 函数来显示错误消息,但它是如果任何验证失败都不会抛出任何错误消息,有人可以解释为什么会这样吗?
UserController.php
<?php
namespace App\Http\Controllers;
use App\Http\Requests\userRequest;
use App\UserSection;
class UserController extends Controller
{
public function userRegister(userRequest $request){
//logic of my code
return response()->json($success);
}
}
userRequest.php
<?php
namespace App\Http\Requests;
use App\Rules\CustomRule;
use Illuminate\Foundation\Http\FormRequest;
class userRequest extends FormRequest
{
public function messages()
{
return [
'first_name.required' => 'A title is required',
];
}
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'first_name' => 'string|required|max:25',
'phone_number' => 'required|integer'
];
}
}
我在没有 first_name 键的情况下到达路线时遇到的错误显示 404 未找到错误
您可能错过了参加 form-data
的 headers 部分
Accept = application/json
If validation fails, a redirect response will be generated to send the
user back to their previous location. The errors will also be flashed
to the session so they are available for display. If the request was
an AJAX request, a HTTP response with a 422 status code will be
returned to the user including a JSON representation of the validation
errors.
所以你需要指定你期望的响应类型,如果你使用 postman 来测试你的 api 端点你必须在请求中添加 header Accept:application/json
<?php
namespace App\Http\Controllers;
use App\Http\Requests\userRequest;
use App\UserSection;
class UserController extends Controller
{
public function userRegister(userRequest $request){
//logic of my code
return response()->json($success);
}
}
我正在使用 laravel 5.8 版本,我有一个 api 负责注册,我创建了一个请求文件,其中包含 rules() 和 messages() 函数来显示错误消息,但它是如果任何验证失败都不会抛出任何错误消息,有人可以解释为什么会这样吗?
UserController.php
<?php
namespace App\Http\Controllers;
use App\Http\Requests\userRequest;
use App\UserSection;
class UserController extends Controller
{
public function userRegister(userRequest $request){
//logic of my code
return response()->json($success);
}
}
userRequest.php
<?php
namespace App\Http\Requests;
use App\Rules\CustomRule;
use Illuminate\Foundation\Http\FormRequest;
class userRequest extends FormRequest
{
public function messages()
{
return [
'first_name.required' => 'A title is required',
];
}
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'first_name' => 'string|required|max:25',
'phone_number' => 'required|integer'
];
}
}
我在没有 first_name 键的情况下到达路线时遇到的错误显示 404 未找到错误
您可能错过了参加 form-data
的 headers 部分Accept = application/json
If validation fails, a redirect response will be generated to send the user back to their previous location. The errors will also be flashed to the session so they are available for display. If the request was an AJAX request, a HTTP response with a 422 status code will be returned to the user including a JSON representation of the validation errors.
所以你需要指定你期望的响应类型,如果你使用 postman 来测试你的 api 端点你必须在请求中添加 header Accept:application/json
<?php
namespace App\Http\Controllers;
use App\Http\Requests\userRequest;
use App\UserSection;
class UserController extends Controller
{
public function userRegister(userRequest $request){
//logic of my code
return response()->json($success);
}
}