Php 如何验证Laravel中其他用户正在更新的用户对象?

Php 如何验证Laravel中其他用户正在更新的用户对象?,php,forms,laravel,request,laravel-5.6,Php,Forms,Laravel,Request,Laravel 5.6,我有一个Laravel应用程序,超级管理员用户可以在其中编辑其他用户 请注意,superadmin也应该能够编辑用户的密码。 因为我不能真正向人们显示密码(反正是散列的),所以我只显示一个空的密码输入字段。验证此用户对象的最佳方法是什么 我的表单如下所示: <form class="form-horizontal" method="post" action="{{ route('update_user') }}"> @csrf <input type="hidd

我有一个Laravel应用程序,超级管理员用户可以在其中编辑其他用户

请注意,superadmin也应该能够编辑用户的密码。 因为我不能真正向人们显示密码(反正是散列的),所以我只显示一个空的密码输入字段。验证此用户对象的最佳方法是什么

我的表单如下所示:

<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>
public function rules()
{
    $userId = $this->input('user_id');
    return [
        'name' => 'sometimes|required|string|max:255',
        'password' => 'sometimes|required|string|min:6|confirmed'
    ];
}
  • 场景是superadmin只编辑name字段并提交表单
  • 收到的密码为空
  • 因此,密码规则给出了一个错误
如果请求的密码值为
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' => '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'
    ];
}

关于可选字段的注释

默认情况下,Laravel包括修剪字符串和 ConvertEmptyStringsToull中间件在应用程序的全局 中间件堆栈。这些中间件在堆栈中由 App\Http\Kernel类。因此,您通常需要进行标记 如果不希望 将空值视为无效的验证器。

必需的\u而不包括所有:foo、bar、

仅当所有其他指定字段均为空时,才显示且不为空 不存在验证中的字段必须为空

如果表单中有任何其他字段,您可以在
required\u中指定其他字段,而不指定\u all:

更新

如果您有许多表单字段,并且希望在没有所有参数的情况下轻松指定
required\u

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;
    }
试试这个

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'
    ];
}

关于可选字段的注释

默认情况下,Laravel包括修剪字符串和 ConvertEmptyStringsToull中间件在应用程序的全局 中间件堆栈。这些中间件在堆栈中由 App\Http\Kernel类。因此,您通常需要进行标记 如果不希望 将空值视为无效的验证器。

必需的\u而不包括所有:foo、bar、

仅当所有其他指定字段均为空时,才显示且不为空 不存在验证中的字段必须为空

如果表单中有任何其他字段,您可以在
required\u中指定其他字段,而不指定\u all:

更新

如果您有许多表单字段,并且希望在没有所有参数的情况下轻松指定
required\u

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;
    }

您可能需要添加一个。这将允许您添加“有时”规则,仅在密码不为空时验证密码:

类UpdateUserRequest扩展了FormRequest
{
公共职能规则()
{
返回[
'name'=>'必需|字符串|最大值:255',
];
}
带验证器的公共函数($validator)
{
//如果密码值不为空,请添加验证规则
$validator->有时('password','required | string | min:6 | confirm',函数($input){
return!空($input->password);
});
}
}
如果您只需要验证的数据,可以根据您的请求调用
validated()
方法:

$user->update($request->validated());

因此,如果密码未经验证(因为它是空的),那么它将不会出现在
validated()
方法返回的数组中。

您可能需要添加密码。这将允许您添加“有时”规则,仅在密码不为空时验证密码:

类UpdateUserRequest扩展了FormRequest
{
公共职能规则()
{
返回[
'name'=>'必需|字符串|最大值:255',
];
}
带验证器的公共函数($validator)
{
//如果密码值不为空,请添加验证规则
$validator->有时('password','required | string | min:6 | confirm',函数($input){
return!空($input->password);
});
}
}
如果您只需要验证的数据,可以根据您的请求调用
validated()
方法:

$user->update($request->validated());

因此,如果密码没有验证(因为它是空的),那么它将不会出现在
validated()
方法返回的数组中。

您不需要用旧密码确认用户凭据吗?不,我不需要它。这是一个封闭的系统,他们的老板将为工人分配密码。当他们忘记的时候;老板会再指派一次。这是一个设计拙劣的流程,但我对此没有发言权。@SühaBoncukçu我添加了一个答案,我认为它更准确地实现了您想要的:您不需要用旧密码确认用户凭据吗?不,我不需要它。这是一个封闭的系统,他们的老板将为工人分配密码。当他们忘记的时候;老板会再指派一次。这是一个设计拙劣的流程,但我对此没有发言权。@SühaBoncukçu我已经添加了一个答案,我认为它更准确地实现了您想要的:抱歉,但问题只包括表单的一个示例。真正的表单要复杂得多。对不起,问题只包括表单的一个示例。真正的表单要复杂得多。我仍然需要取消设置密码值,因为它使用空值通过验证,并尝试将密码字段设置为空,但我会接受这个答案。因为我找不到更好的方法。谢谢,我不想用'requir'