From 501518737c7ef5d5bf3cd9480cd3182714006eff Mon Sep 17 00:00:00 2001 From: Kevin Koech Date: Wed, 11 Sep 2024 12:55:49 +0300 Subject: [PATCH] Update field to use validateUniqueArrayFieldSelect --- .../src/payload/globals/Footer.js | 22 +++++++------------ .../src/payload/globals/Settings.js | 18 ++++++--------- 2 files changed, 15 insertions(+), 25 deletions(-) diff --git a/apps/charterafrica/src/payload/globals/Footer.js b/apps/charterafrica/src/payload/globals/Footer.js index 018b5459b..ced5131f4 100644 --- a/apps/charterafrica/src/payload/globals/Footer.js +++ b/apps/charterafrica/src/payload/globals/Footer.js @@ -1,8 +1,9 @@ import { slateEditor } from "@payloadcms/richtext-slate"; -import { text, select } from "payload/dist/fields/validations"; +import { text } from "payload/dist/fields/validations"; import linkArray from "../fields/linkArray"; import richText from "../fields/richText"; +import validateUniqueArrayFieldSelect from "../utils/validateUniqueArrayFieldSelect"; const socialMediaOptions = [ { @@ -154,19 +155,12 @@ const Footer = { isClearable: false, isSortable: true, }, - validate: (val, options) => { - const { data, t } = options || {}; - if ( - data?.connect?.links?.filter((l) => l.media === val)?.length > - 1 - ) { - return t("charterafrica.site:uniqueMedia"); - } - return select(val, { - ...options, - options: socialMediaOptions, - }); - }, + validate: validateUniqueArrayFieldSelect( + "links", + "media", + socialMediaOptions, + "charterafrica.site:uniqueMedia", + ), }, { name: "url", diff --git a/apps/charterafrica/src/payload/globals/Settings.js b/apps/charterafrica/src/payload/globals/Settings.js index f2be13749..d5bbd7b24 100644 --- a/apps/charterafrica/src/payload/globals/Settings.js +++ b/apps/charterafrica/src/payload/globals/Settings.js @@ -1,6 +1,5 @@ -import { select } from "payload/dist/fields/validations"; - import { locales } from "../utils/locales"; +import validateUniqueArrayFieldSelect from "../utils/validateUniqueArrayFieldSelect"; const Settings = { slug: "settings", @@ -78,15 +77,12 @@ const Settings = { isClearable: false, isSortable: true, }, - validate: (val, options) => { - const { data, t } = options || {}; - if ( - data?.languages?.filter((l) => l.locale === val)?.length > 1 - ) { - return t("charterafrica.site:uniqueLocales"); - } - return select(val, { ...options, options: locales }); - }, + validate: validateUniqueArrayFieldSelect( + "languages", + "locale", + locales, + "charterafrica.site:uniqueLocales", + ), }, { name: "label",