Validate booking contacts: required FIO and email, RF +7 phone mask, and email domain.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import type { CollectionConfig } from 'payload'
|
||||
import { ADMIN_GROUP } from '../admin/groups'
|
||||
import { isValidEmailAddress, isValidFullName, isValidRuPhone, normalizeRuPhone } from '../lib/contact-fields'
|
||||
|
||||
export const Inquiries: CollectionConfig = {
|
||||
slug: 'inquiries',
|
||||
@@ -16,10 +17,45 @@ export const Inquiries: CollectionConfig = {
|
||||
update: ({ req: { user } }) => user?.role === 'admin',
|
||||
delete: ({ req: { user } }) => user?.role === 'admin',
|
||||
},
|
||||
hooks: {
|
||||
beforeValidate: [
|
||||
({ data }) => {
|
||||
if (!data) return data
|
||||
if (typeof data.name === 'string') data.name = data.name.trim().replace(/\s+/g, ' ')
|
||||
if (typeof data.email === 'string') data.email = data.email.trim()
|
||||
if (typeof data.phone === 'string') {
|
||||
const phone = normalizeRuPhone(data.phone)
|
||||
if (phone) data.phone = phone
|
||||
}
|
||||
return data
|
||||
},
|
||||
],
|
||||
},
|
||||
fields: [
|
||||
{ name: 'name', type: 'text', required: true, label: 'ФИО' },
|
||||
{ name: 'email', type: 'email', required: true, label: 'E-mail' },
|
||||
{ name: 'phone', type: 'text', required: true, label: 'Телефон' },
|
||||
{
|
||||
name: 'name',
|
||||
type: 'text',
|
||||
required: true,
|
||||
label: 'ФИО',
|
||||
validate: (value: string | null | undefined) =>
|
||||
isValidFullName(String(value ?? '')) ? true : 'Укажите фамилию и имя буквами',
|
||||
},
|
||||
{
|
||||
name: 'email',
|
||||
type: 'email',
|
||||
required: true,
|
||||
label: 'E-mail',
|
||||
validate: (value: string | null | undefined) =>
|
||||
isValidEmailAddress(String(value ?? '')) ? true : 'Укажите e-mail в формате name@domain.ru',
|
||||
},
|
||||
{
|
||||
name: 'phone',
|
||||
type: 'text',
|
||||
required: true,
|
||||
label: 'Телефон',
|
||||
validate: (value: string | null | undefined) =>
|
||||
isValidRuPhone(String(value ?? '')) ? true : 'Укажите телефон в формате +7 и 10 цифр',
|
||||
},
|
||||
{ name: 'service', type: 'relationship', relationTo: 'services', label: 'Услуга' },
|
||||
{ name: 'doctor', type: 'relationship', relationTo: 'doctors', label: 'Врач' },
|
||||
{ name: 'branch', type: 'relationship', relationTo: 'branches', required: true, label: 'Филиал' },
|
||||
|
||||
Reference in New Issue
Block a user