序列化表单验证 Laravel 8
Serialize form validation Laravel 8
无法验证 laravel 8 中的输入数组。从前端我得到了可以从后端成功访问的序列化表单数据。
public function GetForm(Request $request)
{
$request = $request->all();
$input = array();
parse_str($request['data'], $input);
return $input;
}
output:
{
"inputID":"26",
"inputName":"Julie Hughes est",
"inputPhone":"+94111222333",
"inputEmail":"testmail@gmail.com"
}
我想要的是在用户更改时更新数据之前验证更新函数中的表单。
public function update(Request $request)
{
if (request()->ajax()) {
$request = $this->GetForm($request);
$object = User::find($request['inputID']);
$object->name = $request['inputName'];
$object->phone = $request['inputPhone'];
$object->email = $request['inputEmail'];
$object->save();
return response()->json([
"message" => "User Information has been updated"
], 200);
}
}
下面是我试过的验证函数
public function GetForm(Request $request)
{
$request = $request->all();
$input = array();
parse_str($request['data'], $input);
return $this->validate(
$input, // Expected type 'Illuminate\Http\Request'. Found'array'.intelephense(1006)
[
'inputID' => ['required'],
'inputName' => ['required', 'string', 'max:255'],
'inputPhone' => ['required', 'string', 'email'],
'inputEmail' => ['required', 'min:9', 'number'],
],
);
}
寻找解决此问题的建议。
$('#updateBtn').click(function() {
console.log('update triggered')
axios.get('/users/update/', {
params: {
data: $("#updateForm").serialize()
}
})
.then(function(response) {
let i = response.data
console.log(i);
userTable.ajax.reload();
$('#editModel').modal('hide')
window.toastr.success(i.message);
}).catch(function(error) {
console.log(error)
})
})
试试这样的验证。
public function getForm(Request $request)
{
$inputs = $request->all();
$validator = Validator::make($inputs, [
'inputID' => 'required',
// other validations
]);
if ($validator->passes()) {
return response()->json(['message'=>'User Information has been updated.']);
}
return response()->json(['error'=>$validator->errors()]);
}
Validator::make 使用 Validator facade 为您创建一个验证器实例。而已。您必须自己执行验证并实施错误处理。
无法验证 laravel 8 中的输入数组。从前端我得到了可以从后端成功访问的序列化表单数据。
public function GetForm(Request $request)
{
$request = $request->all();
$input = array();
parse_str($request['data'], $input);
return $input;
}
output:
{
"inputID":"26",
"inputName":"Julie Hughes est",
"inputPhone":"+94111222333",
"inputEmail":"testmail@gmail.com"
}
我想要的是在用户更改时更新数据之前验证更新函数中的表单。
public function update(Request $request)
{
if (request()->ajax()) {
$request = $this->GetForm($request);
$object = User::find($request['inputID']);
$object->name = $request['inputName'];
$object->phone = $request['inputPhone'];
$object->email = $request['inputEmail'];
$object->save();
return response()->json([
"message" => "User Information has been updated"
], 200);
}
}
下面是我试过的验证函数
public function GetForm(Request $request)
{
$request = $request->all();
$input = array();
parse_str($request['data'], $input);
return $this->validate(
$input, // Expected type 'Illuminate\Http\Request'. Found'array'.intelephense(1006)
[
'inputID' => ['required'],
'inputName' => ['required', 'string', 'max:255'],
'inputPhone' => ['required', 'string', 'email'],
'inputEmail' => ['required', 'min:9', 'number'],
],
);
}
寻找解决此问题的建议。
$('#updateBtn').click(function() {
console.log('update triggered')
axios.get('/users/update/', {
params: {
data: $("#updateForm").serialize()
}
})
.then(function(response) {
let i = response.data
console.log(i);
userTable.ajax.reload();
$('#editModel').modal('hide')
window.toastr.success(i.message);
}).catch(function(error) {
console.log(error)
})
})
试试这样的验证。
public function getForm(Request $request)
{
$inputs = $request->all();
$validator = Validator::make($inputs, [
'inputID' => 'required',
// other validations
]);
if ($validator->passes()) {
return response()->json(['message'=>'User Information has been updated.']);
}
return response()->json(['error'=>$validator->errors()]);
}
Validator::make 使用 Validator facade 为您创建一个验证器实例。而已。您必须自己执行验证并实施错误处理。