Laravel验证唯一失败的对象数组更新 [英] Laravel validation unique failing on update for array of object

查看:88
本文介绍了Laravel验证唯一失败的对象数组更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个API,可以发送一组人员,一些是需要更新的现有对象,有些是需要创建的新对象,它们都需要进行验证,并且其中一部分是测试唯一性电子邮件。我正在使用FormRequest:

I have an API that sends an array of staff, some are existing objects that need to be updated and some are new objects that need to be created, they all need to be validated and as part of that is testing for a unique email. I am using a FormRequest:

  $rules = [
        'staff.*.name' => 'required|max:128',
        'staff.*.email' => 'required|email|unique:users',
        'staff.*.description' => 'max:512',            
    ];

所以问题是,正如我确定的那样,该电子邮件地址在更新时无法唯一验证。这是因为,如果ID与要验证的项目相同,则忽略电子邮件的机制会导致我遇到问题。

So the problem is, as I am sure you can see, the email address fails unique validation on update. This is because the mechanism for ignoring the email if the ID is the same as the item being validated is causing me an issue.

我看不到获取当前正在验证的对象ID的方法,因此可以访问其ID。因此,我无法添加该部分:

I can not see a way of getting the ID of the object currently being validated so I can access its ID. So I can't add the part:

'staff.*.email' => 'required|email|unique:users,email,id,' . $currentStaff->id

对于这个特定问题我看不到太多,所以我假设我

I can't see much about this specific issue so I am assuming I am barking up the wrong tree doing it this way or missing something insanely obvious.

下面的有效负载:

{
"staff": [
    {
        "name":"Libbie Turcotte",
        "email":"carolyn16@example.net",
        "updated_at":"2019-12-05 19:28:59",
        "created_at":"2019-12-05 19:28:59",
        "id":53
    },
    {
        "name":"Person Dave",
        "email":"dave@email.com",
    },
    {
        "name":"Staff Name",
        "email":"staff@email.com",

    }
  ]
}


推荐答案

您可以为每个请求人员添加规则元素遍历数组并合并相应的规则:

You can add the rules foreach request staff element looping through the array and merging the corresponding rule:

$rules = [  // this ones are ok for all
    'staff.*.name' => 'required|max:128',
    'staff.*.description' => 'max:512',
];
// here loop through the staff array to add the ignore
foreach($request->staff as $key => $staff) {
    if ( array_key_exists('id', $staff) && $staff['id'] ) { // if have an id, means an update, so add the id to ignore
        $rules = array_merge($rules, ['staff.'.$key.'.email' => 'required|email|unique:users,id,'.$staff['id']]);
    } else {  // just check if the email it's not unique
        $rules = array_merge($rules, ['staff.'.$key.'.email' => 'required|email|unique:users']);
    }
}

因此,对于此请求

staff[1][id]=111
staff[1][email]=dd@ddd.dd
staff[2][id]=222
staff[2][email]=eee@eee.ee
staff[3][email]=fff@ffff

您将具有以下规则:

[
    "staff.*.name" => "required|max:128",
    "staff.*.description" => "max:512",
    "staff.1.email": "required|email|unique:users,id,111",
    "staff.2.email": "required|email|unique:users,id,222",
    "staff.3.email": "required|email|unique:users"
]

这篇关于Laravel验证唯一失败的对象数组更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆