doumen5895 2018-09-07 06:30
浏览 75
已采纳

Laravel 5.6自定义规则验证

I have created a function for validation rule in add/edit "localities"

I have checked the name field as unique in "localities" table.

I have one more field "is_deleted" in localities table. If the "is_deleted" field value is 1, then it didn't ask the validation (i.e) No need to ask "Name is already in use".

 public function rules()
    {
        return [
            'name' => ['required', 'unique:localities'],
            'city_id' => ['required']
        ];
    }

How to create a custom rule for this condition

  • 写回答

1条回答 默认 最新

  • dssnh86244 2018-09-07 06:36
    关注

    You can exclude/ignore by adding column name and id,like

    public function rules()
    {
        return [
            'name' => ['required', 'unique:localities,name,1,is_deleted'],
            'city_id' => ['required']
        ];
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?