doufusi2013 2017-04-12 22:54 采纳率: 0%
浏览 42
已采纳

Laravel 5.4有时会要求验证不要提高“null”输入

I'm having a problem validating inputs that are only going to be present sometimes in the Request.

// Controller
public function update(Request $request, User $user)
{    
    $updateResult = $user->updateUser($request);
    return dd($updateResult);
}

// User Model

protected $validation = [
    'rules' => [
        'email' => [
            'sometimes',
            'email',
            'required',
        ],
        'password' => [
            'sometimes',
            'min:6',
            'required',
        ],
        'first_name' => [
            'sometimes',
            'required',
        ],
        'last_name' => [
            'sometimes',
            'required',
        ],
    ],
    'messages' => [
        'email.required' => 'An email is required.',
        'email.email' => 'The email must be valid.',
        'password.required' => 'A password is required.',
        'password.min' => 'Your password must be at least six (6) characters long.',
        'first_name.required' => 'Your first name is required.',
        'last_name.required' => 'Your last name is required.',
    ],
];

public function updateUser(Request $request)
{
    $validation = Validator::make($request->all(), [
        $this->validation['rules'],
        $this->validation['messages'],
    ]);

    if ($validation->fails())
    {
        return $validation;
    }

    else
    {
        return "OK";
    }
}

So in some update pages $request->all() is only going to have a subset of these fields. However, even a field is present, but the value is null, the required doesn't trigger.

[
    'first_name' => null,
    'last_name' => 'Davidson',
    'job_title' => 'Tech Support',
]

The above request array will return "OK"... If I remove sometimes from the fields, then when a partial input request is sent, it fails saying the fields are required.

I am clearing missing something here, but from reading the docs I thought I'd configured this correctly:

In some situations, you may wish to run validation checks against a field only if that field is present in the input array. To quickly accomplish this, add the sometimes rule to your rule list:

$v = Validator::make($data, [ 'email' => 'sometimes|required|email', ]);

  • 写回答

3条回答 默认 最新

  • douxiong0668 2017-04-13 05:45
    关注

    The problem you are facing is simply due to an error in your call to the validator. The second parameter is not a multidimensional array as you passed. The rules array and the messages array are separate parameters.

    $validation = Validator::make($request->all(), [
        $this->validation['rules'],
        $this->validation['messages'],
    ]);
    

    Should be replaced by

        $validation = Validator::make($request->all(),
            $this->validation['rules'], $this->validation['messages']);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?