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

图片转代码服务由CSDN问答提供 功能建议

我在添加/编辑“地方”中创建了验证规则的功能 我在“地点”表中检查了名称字段是唯一的。

我在地方表中还有一个字段“is_deleted”。 如果“is_deleted”字段值为1,则它不会询问验证(即)无需询问“名称是否已被使用”。

 公共职能规则()
 {
返回[
'name'=>  ['required','unique:localities'],
'nity_id'=>  ['required'] 
]; 
} 
   
 
 

如何为此条件创建自定义规则

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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']
        ];
    }
    
    打赏 评论

相关推荐 更多相似问题