douyu3145 2017-12-05 05:51
浏览 159
已采纳

Laravel:如何在表单请求验证中使用忽略规则

Laravel has a 'unique' rule with an 'except' clause. From the validation documentation, it takes this form:

unique:table,column,except,idColumn

My application has a 'shop' entity. When the user updates the shop's profile, I have a Form Request, with a validation rule configured as follows:

public function rules()
{
    return [
        'shop.name' => 'required|string|max:100|unique:shops,name,except,id',
    ];
}

(The primary key on my shops table is id).

The problem is that Laravel does not take any notice of the 'except' clause. This makes sense (sort of) since the shop ID is not being injected into the Form Request. I can inject id as just another form value, but that doesn't seem to work.

How can I get this rule working in a Form Request?

  • 写回答

2条回答 默认 最新

  • dsvq5069 2017-12-05 06:37
    关注

    To use the except clause to the unique rule, we need to provide the value of the field on the record that we want the rule to ignore directly in the rule itself.

    So, if we want a unique name field for every record except for on the record that the request updates, we need to add the value of the ID field to ignore:

    class UpdateShopRequest extends FormRequest
    {
        ...
        public function rules() 
        {
            return [
                'shop.name' => 'unique:shops,name,' . $this->shop['id'],
            ];
        }
    }
    

    As shown, this rule will cause validation to fail if any row contains the same shop name unless the row's id matches $this->shop['id']. This example assumes that our form contains an array input field for the record's ID attribute because the question is performing validation on an array input:

    <input type="hidden" name="shop[id]" value="{{ $shop->id }}">
    

    ...which lets us fetch the value within the request like we can with any other request. More typically, we pass the record ID as a route parameter, which we can retrieve using the request's route() method:

    $shopId = $this->route('id');
    

    ...which works if we have a route defined similarly to:

    Route::post('shops/{id}', ...);
    

    The fourth parameter to the unique validation rule lets us specify which column the except clause applies to, and defaults to id, so we can leave it off if we're just comparing the record's ID.

    The rule looks a bit clumsy when we just concatenate the column value, especially for a field with a lot of other rules. Since version 5.3, Laravel provides a more elegant syntax to create a unique rule with an except clause:

    use Illuminate\Validation\Rule;
    ...
    return [
        'shop.name' => [ 
            'required', 
            'string', 
            'max:100', 
            Rule::unique('shops', 'name')->ignore($shopId, 'optional_column'), 
        ],
    ];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM