Я использую Laravel FormRequest, чтобы избежать беспорядочного запроса проверки в моем контроллере. Для этого я имею в виду эту статью https://medium.com/@kamerk22 / «умный способ обработки запроса-проверки-в-*» 1026 * -5e8886279271
Проблема заключается в каждом Когда я вызывал метод store, он выдает следующую ошибку
{message: "The given data was invalid.",…}
message: "The given data was invalid."
errors: {affinity_id: ["The affinity id field is required."],…}
affinity_id: ["The affinity id field is required."]
partner_name: ["The partner name field is required."]
tier_id: ["The tier id field is required."]
country_id: ["The country id field is required."]
partner_user_name: ["The partner user name field is required."]
partner_user_email: ["The partner user email field is required."]
partner_user_mobile: ["The partner user mobile field is required."]
contacted_for_id: ["The contacted for id field is required."]
issue_type_id: ["The issue type id field is required."]
comm_mode_id: ["The comm mode id field is required."]
opened_by_id: ["The opened by id field is required."]
assigned_to_id: ["The assigned to id field is required."]
team_id: ["The team id field is required."]
priority_id: ["The priority id field is required."]
tat_id: ["The tat id field is required."]
resolve_date: ["The resolve date field is required."]
status_id: ["The status id field is required."]
description: ["The description field is required."]
Я вызываю метод store, используя ajax
Вот мой код
Контроллер
namespace App\Http\Controllers\tracker;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use App\Http\Requests\TicketStoreRequest;
public function store(TicketStoreRequest $request)
{
$validators = $request->validated();
if ($validators->fails()) {
$data['result'] = false;
$data['messages'] = $validators->errors()->first();
return json_encode($data);
}
}
Форма запроса класса
public function authorize()
{
return true;
}
public function rules()
{
return [
'affinity_id' => 'required',
'partner_name' => 'required',
'tier_id' => 'required',
'country_id' => 'required',
'partner_user_name' => 'required',
'partner_user_email' => 'required',
'partner_user_mobile' => 'required',
'contacted_for_id' => 'required',
'issue_type_id' => 'required',
'comm_mode_id' => 'required',
'opened_by_id' => 'required',
'assigned_to_id' => 'required',
'team_id' => 'required',
'priority_id' => 'required',
'open_date' => 'required',
'tat_id' => 'required',
'resolve_date' => 'required',
'status_id' => 'required',
'description' => 'required'
];
}
Это происходит только в том случае, если правила не выполняются. Если все поля успешно проверены, класс TicketStoreRequest
не выдает ошибку.