首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >规则:notIn([])-没有捕获重复条目吗?

规则:notIn([])-没有捕获重复条目吗?
EN

Stack Overflow用户
提问于 2021-12-08 15:04:47
回答 1查看 117关注 0票数 1

规则:notIn([])-没有捕获重复条目吗?不是为我工作吗?

我有一群人,我不想输入相同的电子邮件地址。如果一个新成员加入,他们需要为该组输入一个唯一的电子邮件地址。$这个->家庭电子邮件已经在组中保存。它是一个一维数组。以下是两个dd()调用的结果。

规则函数(注意,在上面的图形中生成输出的dd()调用被注释掉了:

代码语言:javascript
复制
    public function rules(){
        
//dd('family emails',$this->familyEmails);

        $w = [];
        foreach($this->familyEmails as $femail){
            if($femail != $this->email && array_search(strtolower($femail),$w)==null)
                $w[] = strtolower($femail);
        }

// dd(['rules_array'=>$w]);

        return [
            'email' => ['nullable','string', 'email', 'max:255',Rule::notIn($w)],

        ];

    }

和自定义消息函数

代码语言:javascript
复制
public function customMessages(){
        return [
            
            'email.string' => 'Email Address: The email you entered is not a proper email address; please change.',
            'email.email' => 'Email Address: The email you entered is not a proper email address; please change.',
            'email.max' => 'Email Address: The email you entered is too long; please change.',
            'email.not_in' => 'This email address is already used in the family.  Please enter another Email Address',
            
        ];
    }

我还有一个更新的()函数,它验证动态输入的数据。

代码语言:javascript
复制
public function updated($propertyName){

       $data =  $this->validateOnly($propertyName, $this->rules(), $this->customMessages());
        $this->resetErrorBag($propertyName);
        $this->changesMade = true;
//dd(['data'=>$data,'rules'=>$this->rules()]);
    }

当我在表单中输入电子邮件地址后,当我输入notIn规则中的电子邮件时,您在上面看到的注释掉的dd()将产生以下结果。上面看到的dd()不应该到达,但是它是。

EN

回答 1

Stack Overflow用户

发布于 2021-12-08 15:14:50

好吧,我决定回答我自己的问题,因为它可能对别人有帮助。

我在Rules()函数中创建了Rule:notIn([])。但这引起了问题。

看起来,规则()函数(当然)在每次调用validate()助手时都会运行。答案就在我面前。

当我把循环放在Mount()函数中时,我实现了我的目标。

在我这样做之前,当在Rules()函数中创建规则noIn数组时,它包含了"sam@whistle.com“。当我去为小组中的另一个人输入一个电子邮件地址时,我输入了"sam@whistle.com“。

因此,当我输入重复的电子邮件时,rules()函数运行,并重新创建规则数组,并使用我输入的电子邮件,它将其从规则中删除。我使用了$this->email,它是组件上的公共属性。因此,我不得不将其放入()函数中。

写这篇文章时,我意识到需要将数组放回Rules()函数中,并引用存储电子邮件地址的模型,而不是引用存储类型化值的组件上的公共属性。

代码语言:javascript
复制
        $w = [];
        foreach($this->familyEmails as $femail){
            if($femail != $this->person->email && array_search(strtolower($femail),$w)==null)
                $w[] = strtolower($femail);
        }

而不是$this-> email,我引用电子邮件地址的模型。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70277194

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档