11# Payload CMS Phone Number Plugin
22
33[ ![ Payload CMS] ( https://img.shields.io/badge/Payload%20CMS-v3-blue )] ( https://github.com/payloadcms/payload )
4+ [ ![ NPM Version] ( https://img.shields.io/npm/v/payload-phone-number-plugin )] ( https://www.npmjs.com/package/payload-phone-number-plugin )
45
56This Payload CMS plugin uses [ google-libphonenumber] ( https://github.com/ruimarinho/google-libphonenumber ) to format and validate phone numbers.
67
78![ Preview] ( ./src/assets/preview.png )
89
9- > Admin Panel screenshot of the Phone Number field
10-
1110## Features
12-
13- - Built with Payload UI components so it feels native to the Admin UI
14- - As-you-type formatting
15- - Validates server-side and client-side
16- - Support for limiting to only some countries
11+ - Format phone numbers to multiple formats (E.164, national, international)
12+ - As-you-type formatting based on selected country
13+ - Validates phone numbers based on selected country
14+ - Support for limiting selection to only some countries
1715- Support for setting a default country
18- - Automatic formatting and region detection when pasting phone numbers
16+ - Automatic formatting and region detection when pasting international phone numbers
17+ - Full TypeScript support with generated phone number types
18+ - Built with Payload UI components so it feels native to the Admin Panel
1919- i18n support for validation messages (PRs for new languages are welcome)
2020
2121## Installation
@@ -89,7 +89,7 @@ phoneNumberField({
8989``` ts
9090phoneNumberField ({
9191 name: ' phoneNumber' ,
92- label: ' Phone Phone ' ,
92+ label: ' Phone Number ' ,
9393 defaultCountry: ' NO' , // Norway will be pre-selected
9494 allowedCountries: [' NO' , ' US' , ' SE' ], // Only these countries will be selectable
9595})
@@ -107,9 +107,9 @@ phoneNumberField({
107107})
108108```
109109
110- ## Creating Records Programmatically
110+ ## Creating Documents Programmatically
111111
112- When creating or updating records , pass the phone number as an E.164 string directly.
112+ When creating or updating documents , pass the phone number as an E.164 string directly. Phone numbers are stored in E.164 format in the database .
113113
114114The field will handle parsing and validation so it won't save unless it's a valid phone number for that field.
115115
@@ -139,11 +139,11 @@ await fetch('http://localhost:3000/api/employees', {
139139> [ !NOTE]
140140> You cannot pass a phone number object, only E.164 strings are accepted.
141141
142- ## Querying Phone Numbers by Where
142+ ## Querying by Phone Number
143143
144- When using ` payload.find ` or database queries, use the E.164 format since phone numbers are stored as E.164 strings :
144+ When using ` payload.find ` or database queries, use the E.164 format:
145145
146- ``` typescript
146+ ``` ts
147147const employees = await payload .find ({
148148 collection: ' employees' ,
149149 where: {
@@ -158,7 +158,7 @@ The same applies when using the REST API.
158158
159159## TypeScript Types
160160
161- The plugin uses a union type pattern similar to Payload's relationship fields with depth. The field is typed as ` string | PhoneNumber ` .
161+ The field is typed as ` string | PhoneNumber ` , similar to Payload's relationship fields with depth.
162162
163163This is because phone numbers are stored as strings in the database but are transformed into objects when you read them using libphonenumber.
164164
@@ -192,6 +192,6 @@ Example response:
192192
193193## Additional Information
194194
195- A region code is a ISO 3166-1 alpha-2 code.
195+ A region code is an ISO 3166-1 alpha-2 code.
196196
197197List of all valid region codes can be found here: https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2#Officially_assigned_code_elements
0 commit comments