Kames P
Kames P

Reputation: 35

Laravel 5.6 custom rule validation

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

Upvotes: 1

Views: 151

Answers (1)

Niklesh Raut
Niklesh Raut

Reputation: 34914

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']
    ];
}

Upvotes: 4

Related Questions