Async Manual Inputs

Learn how to use async/await with `manualInputsFn` to load data dynamically before building form inputs.

Async Manual Inputs

Learn how to use async/await with manualInputsFn to load data dynamically before building form inputs. This feature allows you to fetch data from APIs, load configurations, or perform any asynchronous operations before returning the form input array.

A comprehensive guide for using asynchronous operations in manualInputsFn. This guide covers how to make manualInputsFn async, fetch data from APIs, load dropdown options dynamically, and handle loading states.


Overview

The manualInputsFn property in EntityService supports async functions, which allows you to:

  1. Fetch data from APIs - Load dropdown options, configurations, or related data before building the form

  2. Load external configurations - Fetch form configurations from remote sources

  3. Perform async validations - Check data availability or permissions before showing fields

  4. Handle dynamic form building - Build form inputs based on async data dependencies


Use Case 1: Loading Dropdown Options from API

Scenario

You need to load dropdown options from an API before building the form. Instead of using remoteDataConfiguration, you want to fetch the data upfront and populate the options synchronously.

Configuration

Example from Codebase

File: src/app/dashboard/project me/services/projectme.service.ts

Parameters Explained

Parameter
Type
Description
Required

manualInputsFn

(item?: T) => Promise<FormInput<T>[]>

Async function that returns a Promise of form inputs

Required

Return type

Promise<FormInput<T>[]>

Promise that resolves to an array of form inputs

Required

How it Works

  1. Async Function: Declare manualInputsFn as an async function

  2. Await Data: Use await to fetch data before building form inputs

  3. Return Inputs: Return the form inputs array after data is loaded

  4. Form Building: The form library waits for the Promise to resolve before rendering the form


Use Case 2: Loading Multiple Data Sources

Scenario

You need to load data from multiple APIs before building the form, and some fields depend on the loaded data.

Configuration

How it Works

  1. Parallel Loading: Use Promise.all() to load multiple data sources simultaneously

  2. Data Transformation: Transform API responses into the format needed for form inputs

  3. Form Building: Build form inputs with all loaded data available


Use Case 3: Conditional Fields Based on Async Data

Scenario

You need to check permissions or fetch user-specific data to determine which fields should be visible in the form.

Configuration


Use Case 4: Loading Form Configuration from API

Scenario

You want to fetch form field configurations from a remote API, allowing dynamic form structure without code changes.

Configuration


Complete Examples

Example 1: Loading Tags from API (Real Implementation)

This example shows a real implementation from the codebase where tags are loaded from API with fallback mechanism.

File: src/app/dashboard/project me/services/projectme.service.ts

Result:

  • Tags are loaded from API before form renders

  • If API fails, fallback tags are used automatically

  • Form displays tags immediately without waiting for dropdown open

  • Tags are dynamic and can be managed from backend


Example 2: Simple Async Dropdown Loading

Result:

  • Form waits for users to load before rendering

  • Dropdown is populated with user options

  • No need for remoteDataConfiguration since data is loaded upfront


Example 2: Loading with Error Handling


Example 3: Sequential Data Loading with Dependencies


Comparison Table

Approach
Use Case
Pros
Cons

Async manualInputsFn

Load data before form renders

Data available immediately, simpler dropdown config

Form waits for data to load

remoteDataConfiguration

Load data on dropdown open

Faster initial load, lazy loading

More complex configuration

Static options

Fixed, known options

Fastest, no API calls

Not dynamic


Best Practices

  1. Use Promise.all() for parallel loading: When loading multiple independent data sources, load them in parallel

  2. Handle errors gracefully: Always wrap async operations in try-catch blocks

  3. Cache loaded data: Consider caching data if it doesn't change frequently

  4. Use firstValueFrom for HTTP calls: Convert Observables to Promises using firstValueFrom

  5. Provide loading states: Consider showing a loading indicator while form inputs are being built

  6. Type your responses: Use TypeScript types for better type safety


Common Patterns

Pattern 1: Simple API Data Loading

Pattern 2: Parallel Data Loading

Pattern 3: Conditional Field Loading

Pattern 4: Error Handling with Fallback


  • Observable Value - Learn about using Observables in form values

  • Form Input Types - See available form input types

  • Dropdown Configuration - Understand dropdown options


Summary

The manualInputsFn property supports async functions, enabling powerful form building capabilities:

  • Async/await support: Use async functions to load data before building forms

  • API integration: Fetch dropdown options, configurations, or related data

  • Parallel loading: Load multiple data sources simultaneously with Promise.all()

  • Error handling: Gracefully handle API failures with try-catch blocks

  • Conditional fields: Show/hide fields based on async data (permissions, configurations)

This feature enables dynamic form building where form structure and options depend on data loaded from APIs or other async sources, providing flexibility and reducing the need for static configurations.

Last updated