我使用了请求验证,但没有收到来自请求的消息。看我的代码,看看我的错误。唯一重要的是存储功能,如果所有字段都已填写,那么该功能就很好,但是如果没有填写任何字段,我不会从请求中获取我的自定义消息。对于未填写的字段,我不会只给出laravel主页的错误。
这是我的请求文件
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class CarRequest extends FormRequest
{
/**
* 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 [
'car_type' => 'required',
'mark' => 'required',
'model' => 'required',
'fuel' => 'required',
'circuit' => 'required',
'chassis' => 'required|numeric',
'bill_type' => 'required',
'first_registration' => 'required|date',
'km' => 'required|numeric',
'structure' => 'required',
'motor_vehicle_check' => 'required|boolean',
'warranty' => 'required|boolean',
'year' => 'required|date',
'import_vehicle' => 'required|boolean',
'know_damage' => 'required',
'car_accessories' => 'required',
'email' => 'required|email'
];
}
public function messages()
{
return [
'car_type.required' => 'A car_type is required',
'mark.required' => 'A mark is required',
'model.required' => 'A model is required',
'fuel.required' => 'A fuel is required',
'circuit.required' => 'A circuit is required',
'chassis.required' => 'A chassis is required',
'bill_type.required' => 'A bill_type is required',
'first_registration.required' => 'A first_registration is required',
'km.required' => 'A km is required',
'structure.required' => 'A structure is required',
'motor_vehicle_check.required' => 'A motor_vehicle_check is required',
'warranty.required' => 'A warranty is required',
'year.required' => 'A year is required',
'import_vehicle.required' => 'A import_vehicle is required',
'know_damage.required' => 'A know_damage is required',
'car_accessories.required' => 'A car_accessories is required',
'email.required' => 'A email is required'
];
}
}
这是我的控制器代码
<?php
namespace App\Http\Controllers;
use App\Car;
use App\CarImages;
use App\Http\Requests\CarRequest;
use Illuminate\Http\Request;
use Carbon\Carbon;
use Illuminate\Support\Facades\Config;
class CarController extends Controller
{
public function index()
{
$cars = Car::with(['images'])
->orderByDesc('car.created')
->get();
return response()->json($cars, 200);
}
public function search($name){
$cars = Car::where('mark', '=' , $name)->get();
return $this->response($cars);
}
public function create()
{
//
}
public function show($id)
{
$car = School::with(['images'])->find($id);
if (!$car) {
return response()->json(['message' => 'No Car found'], 404);
}
return response()->json($car, 200);
}
public function store(CarRequest $request)
{
$car = Car::create([
'car_type' => $request->input('car_type'),
'mark' => $request->input('mark'),
'model' => $request->input('model'),
'fuel' => $request->input('fuel'),
'circuit' => $request->input('circuit'),
'chassis' => $request->input('chassis'),
'bill_type' => $request->input('bill_type'),
'first_registration' => $request->input('first_registration'),
'km' => $request->input('km'),
'structure' => $request->input('structure'),
'motor_vehicle_check' => $request->input('motor_vehicle_check'),
'warranty' => $request->input('warranty'),
'year' => $request->input('year'),
'import_vehicle' => $request->input('import_vehicle'),
'know_damage' => $request->input('know_damage'),
'car_accessories' => $request->input('car_accessories'),
'email' => $request->input('email')
]);
return response()->json([
'message' => 'Your car has been successfully added',
'car' => $car
],201);
}
public function destroy($id)
{
$car = Car::destroy($id);
return response()->json($id);
}
}
答案 0 :(得分:1)
我使用了请求验证,但没有收到来自请求的消息。
在期待json响应时,请不要在发出请求时添加此标头:
Accept: Application/json // <--
如果我想在自定义消息示例中设置km必须为数字,那么消息中的功能如何?
您需要为每个这样的规则指定消息。让我们来进行km
验证:
MyCustomRequest.php
public function rules()
{
return [
// ...
// 'first_registration' => 'required|date',
'km' => 'required|numeric', // <---
// 'structure' => 'required',
// ...
];
}
假设km
有两个验证,只需在messages()
函数中再添加一个元素即可:
MyCustomRequest.php
public function messages()
{
return [
// ...
'km.required' => 'A km is required',
'km.numeric' => 'The km needs to be numeric, dude!', // <---
// ...
];
}
关于最后一个主题,请选中the documentation。
答案 1 :(得分:0)
据我所知,您正确地完成了所有操作。您唯一错过的是store()方法中的一个:
$validator = $request->validated();
就在方法主体的开头