Aphex
Schema Types

Reference

Links to other documents with a searchable picker.

The reference type creates a relationship between documents. It renders a searchable dropdown that lets users pick from existing documents of the specified type.

Definition

import type { ReferenceField } from '@aphexcms/cms-core';
{
  name: 'author',
  type: 'reference',
  title: 'Author',
  to: [{ type: 'person' }],
  validation: (Rule) => Rule.required()
}

Properties

PropertyTypeRequiredDescription
namestringYesField identifier.
type'reference'YesMust be 'reference'.
titlestringYesLabel shown in the admin UI.
descriptionstringNoHelp text shown below the label.
toArray<{ type: string }>YesDocument types this field can reference. Currently only the first type in the array is used by the component.
initialValueany | () => any | Promise<any>NoDefault value.
validation(Rule) => RuleNoValidation rules.

Examples

Single type reference

{
  name: 'category',
  type: 'reference',
  title: 'Category',
  to: [{ type: 'category' }],
  validation: (Rule) => Rule.required()
}

Author reference in a blog post

const post: SchemaType = {
	type: 'document',
	name: 'post',
	title: 'Blog Post',
	fields: [
		{ name: 'title', type: 'string', title: 'Title' },
		{
			name: 'author',
			type: 'reference',
			title: 'Author',
			to: [{ type: 'author' }],
			validation: (Rule) => Rule.required()
		},
		{ name: 'body', type: 'text', title: 'Body' }
	]
};

UI Features

When no reference is selected:

  • Searchable dropdown showing documents of the target type (first 10 results)
  • "Create new" button to create a referenced document on the fly

When a reference is selected:

  • Shows the referenced document's title, type, and status (draft/published)
  • Button to open the referenced document in the editor
  • Button to clear the selection

Stored Data

References store the ID of the referenced document as a string:

{
	author: 'document-id-123';
}

Validation

Available rules: required(), custom(fn), error(msg), warning(msg), info(msg)

Edit on GitHub

Last updated on