laravel 中用于更新 table 的自定义验证
Custom validation in laravel for updating the table
我正在尝试验证 laravel 5.4 中的更新方法 以下是我的验证代码:
$data = $request->only('id', 'name', 'number', 'logo', 'email', 'address', 'city', 'state', 'country', 'type', 'tier', 'is_client');
$company = Companies::find($request->id);
Validator::make($data, [
'name' => 'required|max:255',
'email' => 'required', Rule::unique('companies')->ignore($company->id),
'address' => 'max:255',
'city' => 'max:255',
'state' => 'max:255',
'country' => 'max:255',
]);
但是这个验证失败了,假设在更新方法中我将名称传递给空它没有抛出错误代码 422
显示此错误,我无法捕获错误并显示在 span 标记中
更新
按照答案中的建议,我尝试添加验证并尝试检查我是否收到以下错误:
Trying to get property of non-object
以下是我更新后的代码:
$data = $request->only(['id', 'name', 'number', 'logo', 'email', 'address', 'city', 'state', 'country', 'type', 'tier', 'is_client']);
$company = Companies::find($request->id);
Validator::make($data, [
'name' => 'required|max:255',
'email' => 'required', Rule::unique('companies')->ignore($company->id),
'address' => 'max:255',
'city' => 'max:255',
'state' => 'max:255',
'country' => 'max:255',
])->validate();
即使我不将 $data = request->only()
设为数组,我也会得到同样的错误。
->only()
需要一个参数数组:
$request->only(['id', 'name', 'number', 'logo', 'email', 'address', 'city', 'state', 'country', 'type', 'tier', 'is_client']);
您传递了几个参数。
您只是在创建 Validator
,并未验证数据。必须调用 validate
函数。
Validator::make($data, [
...
])->validate();
更新
错误 Trying to get property of non-object
。请注意,您将电子邮件验证规则作为字符串传递,它应该是 array
'email' => ['required', Rule::unique('companies')->ignore($company->id)],
如果错误仍然存在。检查变量 $company
是否包含数据。
我正在尝试验证 laravel 5.4 中的更新方法 以下是我的验证代码:
$data = $request->only('id', 'name', 'number', 'logo', 'email', 'address', 'city', 'state', 'country', 'type', 'tier', 'is_client');
$company = Companies::find($request->id);
Validator::make($data, [
'name' => 'required|max:255',
'email' => 'required', Rule::unique('companies')->ignore($company->id),
'address' => 'max:255',
'city' => 'max:255',
'state' => 'max:255',
'country' => 'max:255',
]);
但是这个验证失败了,假设在更新方法中我将名称传递给空它没有抛出错误代码 422
显示此错误,我无法捕获错误并显示在 span 标记中
更新
按照答案中的建议,我尝试添加验证并尝试检查我是否收到以下错误:
Trying to get property of non-object
以下是我更新后的代码:
$data = $request->only(['id', 'name', 'number', 'logo', 'email', 'address', 'city', 'state', 'country', 'type', 'tier', 'is_client']);
$company = Companies::find($request->id);
Validator::make($data, [
'name' => 'required|max:255',
'email' => 'required', Rule::unique('companies')->ignore($company->id),
'address' => 'max:255',
'city' => 'max:255',
'state' => 'max:255',
'country' => 'max:255',
])->validate();
即使我不将 $data = request->only()
设为数组,我也会得到同样的错误。
->only()
需要一个参数数组:
$request->only(['id', 'name', 'number', 'logo', 'email', 'address', 'city', 'state', 'country', 'type', 'tier', 'is_client']);
您传递了几个参数。
您只是在创建 Validator
,并未验证数据。必须调用 validate
函数。
Validator::make($data, [
...
])->validate();
更新
错误 Trying to get property of non-object
。请注意,您将电子邮件验证规则作为字符串传递,它应该是 array
'email' => ['required', Rule::unique('companies')->ignore($company->id)],
如果错误仍然存在。检查变量 $company
是否包含数据。