ajax的Laravel表单验证返回错误



我正在尝试使用ajax将数据插入数据库。在此之前,我需要验证表单。我正在将序列化的表单数据传递给控制器。但当我试图验证时,它会给我像"The given data was invalid."一样的错误

这是我的代码

ajax功能

$('#btn-save-appointment').on('click', function(e) {
e.preventDefault();
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
}
});

jQuery.ajax({
url: "/saveAppointment",
method: 'post',
data: { formdata: $('#appointment-event').serialize() },
cache: false,
processData : false,
success: function(result){ 
console.log('form: '+result);            

}
});
});

控制器

public function saveAppointment(AppointmentStoreRequest $request) {         
$validated = $request->validated();
dd($validated);
}

预约存储请求

namespace AppHttpRequests;
use IlluminateFoundationHttpFormRequest;
class AppointmentStoreRequest extends FormRequest
{
public function authorize()
{
return true;
}
public function rules()
{
return [
'category' => 'required',
'treatments' => 'required',
'time-in-minutes' => 'required',
'treatmentsRooms' => 'required',
'date' => 'required',
'time' => 'required',
'gender' => 'required',
'first_name' => 'sometimes|required',
'last_name' => 'sometimes|required',
'dob' => 'sometimes|required',
'insurance' => 'sometimes|required',
'phone' => 'sometimes|required',
'email' => 'sometimes|required|email'
];
}
}

data属性中删除{ formdata: ... }

data: $('#appointment-event').serialize(),

相关内容

  • 没有找到相关文章

最新更新