我应该如何验证Laravel中另一个用户正在更新的用户对象?

时间:2018-04-14 15:42:50

标签: php forms laravel request laravel-5.6

我有一个Laravel应用程序,其中superadmin用户可以编辑其他用户。

请注意,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>

我在FormRequest中的规则如下所示:

public function rules()
{
    $userId = $this->input('user_id');
    return [
        'name' => 'sometimes|required|string|max:255',
        'password' => 'sometimes|required|string|min:6|confirmed'
    ];
}
  • 场景是superadmin只编辑名称字段并提交表单。
  • 密码已收到null。
  • 因此密码规则会出错。

如果是null,我可以通过取消设置请求的密码值来解决这个问题。但我真诚地相信这是一种蹩脚的方式。有没有更好的方法来实现这一目标?

3 个答案:

答案 0 :(得分:1)

试试这个..

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

doc

  

关于可选字段的说明

     

默认情况下,Laravel包含TrimStrings和   ConvertEmptyStringsToNull应用程序的全局中间件   中间件堆栈。这些中间件列在堆栈中   App \ Http \ Kernel类。因此,您经常需要标记   你的&#34;可选&#34;如果你不想要,请求字段为可空的   验证器将空值视为无效。

doc

  

<强> required_without_all:FOO,酒吧,...

     

仅在所有其他指定字段都存在时才显示且不为空   不存在验证字段必须

如果表格中有任何其他字段,您可以在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;
    }

答案 1 :(得分:1)

您可能想要添加“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()方法返回的数组中。

答案 2 :(得分:0)

我希望这有效:

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

通过这种方式,您可以单独验证这两个字段。空请求是不可接受的。它应该具有两个值中的任何一个。