Skip to content

CommandForm

The CommandForm component provides a declarative way to build forms for Arc commands with built-in validation, error handling, and field management.

CommandForm simplifies working with Arc commands in React by:

  • Automatically managing command state
  • Providing type-safe field bindings
  • Handling validation integration
  • Supporting flexible customization
  • Managing form lifecycle
import { CommandForm } from '@cratis/arc.react/commands';
import { InputTextField } from '@cratis/arc.react/commands';
class UserCommand extends Command {
name = '';
email = '';
}
function MyForm() {
return (
<CommandForm command={UserCommand}>
<InputTextField<UserCommand>
value={c => c.name}
title="Name"
placeholder="Enter your name"
/>
<InputTextField<UserCommand>
value={c => c.email}
title="Email"
type="email"
placeholder="Enter your email"
/>
<button type="submit">Submit</button>
</CommandForm>
);
}

Note: Field components require an explicit generic type parameter (e.g., <InputTextField<UserCommand>>) to ensure the value accessor function is properly typed. This provides full IntelliSense and type safety when writing c => c.propertyName.

PropertyTypeDefaultDescription
commandConstructor<TCommand>RequiredThe command class to use for the form
initialValuesPartial<TCommand>undefinedInitial values for the form fields
currentValuesPartial<TCommand>undefinedCurrent values that will be merged with initial values
showTitlesbooleantrueWhether to show field titles automatically
showErrorsbooleantrueWhether to show field error messages automatically
fieldContainerComponentReact.ComponentType<FieldContainerProps>undefinedCustom component for rendering field containers
fieldDecoratorComponentReact.ComponentType<FieldDecoratorProps>undefinedCustom component for decorating fields with icons and tooltips
errorDisplayComponentReact.ComponentType<ErrorDisplayProps>undefinedCustom component for rendering validation errors
tooltipComponentReact.ComponentType<TooltipWrapperProps>undefinedCustom component for rendering tooltips on field descriptions
errorClassNamestring'p-error'CSS class name for error message elements
iconAddonClassNamestring'p-inputgroup-addon'CSS class name for icon addon containers
onFieldValidate(command, fieldName, oldValue, newValue) => string | undefinedundefinedCustom field validation function
onFieldChange(command, fieldName, oldValue, newValue) => voidundefinedCallback when field value changes
onBeforeExecute(values) => valuesundefinedTransform command values before execution
onSuccess(response: TResponse) => voidundefinedCalled when command executes successfully
onFailed(commandResult: CommandResult<TResponse>) => voidundefinedCalled when command execution fails
onException(messages: string[], stackTrace: string) => voidundefinedCalled when command throws an exception
onUnauthorized() => voidundefinedCalled when user is not authorized
onValidationFailure(validationResults: ValidationResult[]) => voidundefinedCalled when command fails validation

Set initial values for the form:

<CommandForm
command={UserCommand}
initialValues={{
name: 'John Doe',
email: 'john@example.com',
role: 'user'
}}
>
<InputTextField<UserCommand> value={c => c.name} title="Name" />
<InputTextField<UserCommand> value={c => c.email} title="Email" />
<SelectField<UserCommand> value={c => c.role} title="Role" options={roles} />
</CommandForm>

CommandForm accepts any React elements as children. The following are treated specially:

  • Field components - Components with displayName of 'CommandFormField' are automatically bound to the command
  • CommandForm.Column - Used for multi-column layouts
  • Other elements - Headings, buttons, divs, etc. are rendered as-is in order