久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

Laravel 表單驗證唯一使用 2 個字段

Laravel form validation unique using 2 fields(Laravel 表單驗證唯一使用 2 個字段)
本文介紹了Laravel 表單驗證唯一使用 2 個字段的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

如何在 2 個字段上設置唯一的驗證規則?

How can I have a unique validation rule on 2 fields?

一個.應用程序不應允許兩個人具有相同的名字和姓氏.

a. The application should not allow two people to have the same identical first name and last name.

允許用戶只填寫名字或只填寫姓氏.因為用戶可能只有其中之一.

It is allowed that the users fills in only a first name or only a last name. Because the user may have only one of them.

B.但是,如果用戶只輸入名字 (Glen),則表中的其他人不應具有相同的名稱(名字 = 'Glen' 且姓氏 = null).另一個格倫史密斯"還可以.

b. But if the user enters only a first name (Glen), no other person in the table should have the same (first name = 'Glen' and last name = null). another 'Glen Smith' ok.

我嘗試了以下規則.當兩個字段(名字和姓氏)都不為空時,它工作得很好:

I tried the following rule. It works great when both fields (first and last name) are not null:

'firstName' => 'unique:people,firstName,NULL,id,lastName,' . $request->lastName

此規則在 b 上失敗.當只有一個字段時.

This rule fails on b. when only one field is present.

有什么提示嗎?

推薦答案

內置的 unique 驗證器不會真正支持您嘗試執行的操作.它的目的是確保單個有效值在數據庫中是唯一的,而不是兩個值的組合.但是,您可以創建一個自定義驗證器:

The built in unique validator wouldn't really support what you're trying to do. It's purpose is to ensure that a single valid is unique in the database, rather than a composite of two values. However, you can create a custom validator:

Validator::extend('uniqueFirstAndLastName', function ($attribute, $value, $parameters, $validator) {
    $count = DB::table('people')->where('firstName', $value)
                                ->where('lastName', $parameters[0])
                                ->count();

    return $count === 0;
});

然后您可以通過以下方式訪問此新規則:

You could then access this new rule with:

'firstName' => "uniqueFirstAndLastName:{$request->lastName}"

您可能會發現您可能需要稍微調整您的數據庫查詢,因為它未經測試.

You'll probably find you might need to tweak your database query a little bit as it's untested.

這篇關于Laravel 表單驗證唯一使用 2 個字段的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Magento products by categories(按類別劃分的 Magento 產品)
Resource interpreted as image but transferred with MIME type text/html - Magento(資源被解釋為圖像但使用 MIME 類型 text/html 傳輸 - Magento)
Is there an event for customer account registration in Magento?(Magento 中是否有客戶帳戶注冊事件?)
Magento addFieldToFilter: Two fields, match as OR, not AND(Magento addFieldToFilter:兩個字段,匹配為 OR,而不是 AND)
quot;Error 404 Not Foundquot; in Magento Admin Login Page(“未找到錯誤 404在 Magento 管理員登錄頁面)
Get Order Increment Id in Magento(在 Magento 中獲取訂單增量 ID)
主站蜘蛛池模板: www.久草| 精久久 | 一区二区三区电影在线观看 | 国产精品视频在线免费观看 | 色综合色综合色综合 | 亚洲欧美aⅴ| 色婷婷综合网 | 免费观看一级特黄欧美大片 | 亚洲区一区二 | 在线日韩精品视频 | 久久国内精品 | 在线观看视频91 | 97超碰人人| 孕妇一级毛片 | 欧美成年黄网站色视频 | 欧美综合国产精品久久丁香 | 国产精品成人av | 国产aa| 欧美区精品 | 狠狠干影院 | 国产成人av在线播放 | 国产一级一级国产 | 国产亚洲精品精品国产亚洲综合 | 国产永久免费 | 久久久久亚洲 | 波多野结衣中文字幕一区二区三区 | 91麻豆精品国产91久久久资源速度 | 一区二区三区免费 | 精品国产欧美一区二区三区不卡 | 国产精品夜夜春夜夜爽久久电影 | 国产精品久久久久久久久久不蜜臀 | 欧美v在线 | 免费一区二区三区 | 一区二区三区免费观看 | 最新日韩欧美 | 国产精品污www一区二区三区 | 久久精品中文 | 久久免费精品 | 日韩在线免费 | 中文字幕在线欧美 | 国产91精品久久久久久久网曝门 |