将带有表单的Ajax请求发送到控制器并在其中进行验证,但是即使字段中已经填充了数据,也仍然显示错误。
我的ajax请求
var formData = form.serialize();
$.ajax({
type: "POST",
url: url,
method: 'post',
data: {"data": formData},
headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
dataType: "json",
})
我的控制器功能
public function store(Request $req)
{
parse_str($req->formData, $data);
$validator = Validator::make($data, [
'hotel_name' => 'required',
'company_name' => 'required|numeric',
'hotel_country' => 'required',
'hotel_city' => 'required|alpha',
'hotel_area' => 'required|alpha',
'hotel_address' => 'required|alpha_dash'
]);
if ($validator->fails()) {
return array(
'success' => false,
'errors' => $validator->getMessageBag()->toArray()
);
}
}
继续返回假