Laravel uniquewith-validator包可为空的功能将无法正常工作 [英] Laravel uniquewith-validator package nullable function won't work

查看:44
本文介绍了Laravel uniquewith-validator包可为空的功能将无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是uniquewith-validator程序包,并且设法设置为不允许这三个组合:姓名,姓氏和电话.问题是当我想输入相同的名字和姓氏但没有电话时.如果电话为空或为空,则表示它是重复的,但是如果我更换电话,则应允许.我需要允许,但是在此代码中不允许:

I'm using uniquewith-validator packageand I manage to set not to allow these three combination: name, last_name and telephone. The problem is when I want to enter same name and last_name but without telephone. When telephone is empty or null it says it's duplicate but if I change telephone it allows it as it should. I need to allow that but in this code it doesn't:

'name' => 'required|unique_with:add_members, last_name, telephone'

当我使一列唯一时,我使用可为空的函数,但在这里它不起作用...

When I make one column unique I use nullable function but here it doesn't work...

当我将一列设为唯一并且可以正常工作时,我使用nullable:

I use nullable when I make one column unique and this is working:

's_number' => 'nullable|unique:add_members'

如何允许电话可以为空并且不算作重复电话?

How to allow telephone to be nullable and not to count as duplicate?

这是我在AddMemberRequest.php中的完整代码

This is my full code in AddMemberRequest.php

public function rules()
{
    return [
        'name'          => 'required|nullable|unique_with:add_members, last_name, telephone',
        'last_name'     => 'required',
        's_number'      => 'nullable|unique:add_members',
        'street'        => 'nullable|unique_with:add_members, name, last_name, number',
    ];
}

推荐答案

更改属性后,我成功了.代替设置要求时我已经使用的名称属性,我添加了电话属性,使其与名称和last_name组合在一起是唯一的并且可以为空.像这样:

I succeeded when I changed the attribute. Instead of name attribute that I allready use when I set to be required I add telephone attribute to be unique in combination with name and last_name and to be nullable. Like this:

'telephone_1'   => 'nullable|unique_with:add_members, name, last_name',

现在完整的规则功能是:

And full rules function now is:

public function rules()
{
    return [
        'name' => 'required',
        'last_name' => 'required',
        's_number' => 'nullable|unique:add_members',
        'telephone_1' => 'nullable|unique_with:add_members, name, last_name',
        'street' => 'nullable|unique_with:add_members, name, last_name, number',
    ];
}

我希望有人会对这个解决方案有所帮助.

I hope somebody will be helpful this solution.

这篇关于Laravel uniquewith-validator包可为空的功能将无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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