我应该如何验证 Laravel 中另一个用户正在更新的用户对象?
How should I validate a user object being updated by another user in Laravel?
我有一个 Laravel 应用程序,超级管理员用户可以在其中编辑其他用户。
请注意超级管理员也应该能够编辑用户密码。
由于我无法真正向人们显示密码(无论如何它都是散列的)我只显示一个空的密码输入字段。验证此用户对象的最佳方法是什么?
我的表单如下所示:
<form class="form-horizontal" method="post" action="{{ route('update_user') }}">
@csrf
<input type="hidden" name="user_id" value="{{ $user->id }}">
<input type="text" name="name" value="{{ $user->name }}">
<input type="password" name="password" >
<button type="submit">
</form>
我在 FormRequest 中的规则如下所示:
public function rules()
{
$userId = $this->input('user_id');
return [
'name' => 'sometimes|required|string|max:255',
'password' => 'sometimes|required|string|min:6|confirmed'
];
}
- 场景是超级管理员只编辑名称字段并提交表单。
- 收到的密码为空。
- 所以密码规则报错
如果是 null
,我可以通过取消设置请求中的密码值来处理这个问题。但我真诚地认为这是一种蹩脚的做法。有没有更好的方法来实现这个?
我希望这有效:
public function rules()
{
$userId = $this->input('user_id');
return [
'name' => 'required_if:password,null|string|max:255',
'password' => 'required_if:name,null|string|min:6|confirmed'
];
}
通过这种方式,您可以分别验证这两个字段。空请求是不可接受的。它应该具有两个值之一。
试试这个..
public function rules()
{
$userId = $this->input('user_id');
return [
'name' => 'nullable|required_without_all:password,anotherfield|string|max:255',
'password' => 'nullable|required_without_all:name,anotherfield|string|min:6|confirmed'
];
}
A Note On Optional Fields
By default, Laravel includes the TrimStrings and
ConvertEmptyStringsToNull middleware in your application's global
middleware stack. These middleware are listed in the stack by the
App\Http\Kernel class. Because of this, you will often need to mark
your "optional" request fields as nullable if you do not want the
validator to consider null values as invalid.
required_without_all:foo,bar,...
present and not empty only when all of the other specified fields are
not presentThe field under validation must be
如果表单中有任何其他字段,您可以在 required_without_all:
中指定其他字段。
更新
如果您有很多表单域并且想轻松指定required_without_all
个参数。
public function rules()
{
$userId = $this->input('user_id');
return [
'name' => [
'nullable',
'required_without_all:'. $this->requiredWithout('name'),
'string',
'max:255',
],
'password' => [
'nullable',
'required_without_all:'. $this->requiredWithout('password'),
'string',
'min:6',
'confirmed'
]
];
}
public function requiredWithout($currentField) {
$requiredWithoutValue = "";
foreach ($this->request->all() as $key => $value) {
//excluding _token as it will be always not empty value
if($key != '_token' && $key != $currentField) {
$requiredWithoutValue = $vrequiredWithoutValue. $key. ",";
}
}
return $requiredWithoutValue;
}
您可能想添加一个 “after” validation hook。这将允许您添加“有时”规则以仅在密码不为空时验证密码:
class UpdateUserRequest extends FormRequest
{
public function rules()
{
return [
'name' => 'required|string|max:255',
];
}
public function withValidator($validator)
{
// If password value is not empty, add the validation rules
$validator->sometimes('password', 'required|string|min:6|confirmed', function ($input) {
return ! empty($input->password);
});
}
}
如果您随后只需要经过验证的数据,则可以根据您的要求调用 validated()
方法:
$user->update($request->validated());
因此,如果密码未通过验证(因为它留空),则它不会出现在 validated()
方法返回的数组中。
我有一个 Laravel 应用程序,超级管理员用户可以在其中编辑其他用户。
请注意超级管理员也应该能够编辑用户密码。 由于我无法真正向人们显示密码(无论如何它都是散列的)我只显示一个空的密码输入字段。验证此用户对象的最佳方法是什么?
我的表单如下所示:
<form class="form-horizontal" method="post" action="{{ route('update_user') }}">
@csrf
<input type="hidden" name="user_id" value="{{ $user->id }}">
<input type="text" name="name" value="{{ $user->name }}">
<input type="password" name="password" >
<button type="submit">
</form>
我在 FormRequest 中的规则如下所示:
public function rules()
{
$userId = $this->input('user_id');
return [
'name' => 'sometimes|required|string|max:255',
'password' => 'sometimes|required|string|min:6|confirmed'
];
}
- 场景是超级管理员只编辑名称字段并提交表单。
- 收到的密码为空。
- 所以密码规则报错
如果是 null
,我可以通过取消设置请求中的密码值来处理这个问题。但我真诚地认为这是一种蹩脚的做法。有没有更好的方法来实现这个?
我希望这有效:
public function rules()
{
$userId = $this->input('user_id');
return [
'name' => 'required_if:password,null|string|max:255',
'password' => 'required_if:name,null|string|min:6|confirmed'
];
}
通过这种方式,您可以分别验证这两个字段。空请求是不可接受的。它应该具有两个值之一。
试试这个..
public function rules()
{
$userId = $this->input('user_id');
return [
'name' => 'nullable|required_without_all:password,anotherfield|string|max:255',
'password' => 'nullable|required_without_all:name,anotherfield|string|min:6|confirmed'
];
}
A Note On Optional Fields
By default, Laravel includes the TrimStrings and ConvertEmptyStringsToNull middleware in your application's global middleware stack. These middleware are listed in the stack by the App\Http\Kernel class. Because of this, you will often need to mark your "optional" request fields as nullable if you do not want the validator to consider null values as invalid.
required_without_all:foo,bar,...
present and not empty only when all of the other specified fields are not presentThe field under validation must be
如果表单中有任何其他字段,您可以在 required_without_all:
中指定其他字段。
更新
如果您有很多表单域并且想轻松指定required_without_all
个参数。
public function rules()
{
$userId = $this->input('user_id');
return [
'name' => [
'nullable',
'required_without_all:'. $this->requiredWithout('name'),
'string',
'max:255',
],
'password' => [
'nullable',
'required_without_all:'. $this->requiredWithout('password'),
'string',
'min:6',
'confirmed'
]
];
}
public function requiredWithout($currentField) {
$requiredWithoutValue = "";
foreach ($this->request->all() as $key => $value) {
//excluding _token as it will be always not empty value
if($key != '_token' && $key != $currentField) {
$requiredWithoutValue = $vrequiredWithoutValue. $key. ",";
}
}
return $requiredWithoutValue;
}
您可能想添加一个 “after” validation hook。这将允许您添加“有时”规则以仅在密码不为空时验证密码:
class UpdateUserRequest extends FormRequest
{
public function rules()
{
return [
'name' => 'required|string|max:255',
];
}
public function withValidator($validator)
{
// If password value is not empty, add the validation rules
$validator->sometimes('password', 'required|string|min:6|confirmed', function ($input) {
return ! empty($input->password);
});
}
}
如果您随后只需要经过验证的数据,则可以根据您的要求调用 validated()
方法:
$user->update($request->validated());
因此,如果密码未通过验证(因为它留空),则它不会出现在 validated()
方法返回的数组中。