Field Metadata
Entities in Remult are not just a single source of truth for storage, APIs, and authentication—they can serve as a central point for managing any entity-related aspect across your application. Let’s explore how to utilize field metadata for consistent UI labeling and data formatting.
Setting Captions
To ensure consistent captions across your app, set the caption
attribute directly in the field definition. This way, the same caption is automatically used wherever the field appears.
Accessing Captions
Field metadata, like caption
, can be accessed using the fields
property of a repository:
Using captions this way allows for a unified UI. For example, in TodoItem.tsx
:
Try changing the caption of title
in Task
and observe how the UI updates automatically!
Display Consistency with displayValue
To ensure consistent display formatting, especially for literals or dates, use the displayValue
property.
Example 1: Displaying a Literal Field
For fields with literal values, like priority
, displayValue
can ensure consistent capitalization:
In TodoItem.tsx
, access and use this display formatting:
Example 2: Displaying Dates with Reusable displayValue
Let’s take a closer look at defining displayValue
for dates:
In this example, the displayValue
function is designed to ignore the first parameter (representing the entity) and only use the second parameter, the date value. By focusing on the value alone, this displayValue
function can be refactored into a standalone utility that works for any date field, not just createdAt
.
Refactoring displayValue
for Reusability
You can create a reusable displayDate
function and use it across different date fields:
Now, any date field can use this displayDate
function for consistent date formatting, regardless of the entity:
This approach ensures consistency in date formatting across your application and keeps your code clean and maintainable. You can define similar reusable functions for other field types, ensuring that formatting stays uniform across different entities and fields.
Extending Field Options with Custom Options
Beyond the standard options, fields can also be enhanced with custom options tailored to your application’s specific needs. These options allow you to store and access any metadata you might need directly within the field definition, making your entity models even more powerful and adaptable.
For example, you might want to add a custom validation message, tooltip, or any other metadata to a field. This added flexibility helps you centralize and standardize additional properties that can be useful in various parts of your application, from dynamic UI rendering to custom business logic.
To explore more about how to define and use custom options, check out Enhancing Field and Entity Definitions with Custom Options.
Leveraging Metadata for Dynamic UI
With field metadata, you can abstract your UI components for consistent display across your app. Here’s an example of a dynamic component that uses field metadata:
Click “Solve” at the top right of the code editor to see this abstraction in action. This dynamic UI approach ensures your fields are displayed with the same metadata-defined captions and formatting throughout the app.
you can use the getValueList
function to get the values of a literal field
You can use these capabilities, together with the structured error model to create dynamic forms, dynamic grid and other dynamic uis that leverage
- Installing dependencies
- Starting http server